classSolution{// Trie node for storing prefix XOR values in binary formclassTrieNode{TrieNode[]children=newTrieNode[2];// 0 and 1 branchesintcount=0;// number of prefix values passing through this node}TrieNoderoot=newTrieNode();// Insert or remove a prefix XOR value from the trievoidupdateTrie(intvalue,intdelta){TrieNodecurrent=root;for(intbit=14;bit>=0;bit--){intcurrentBit=(value>>bit)&1;if(current.children[currentBit]==null){current.children[currentBit]=newTrieNode();}current=current.children[currentBit];current.count+=delta;}}// Find maximum XOR of given value with any value currently in the trieintgetMaxXor(intvalue){TrieNodecurrent=root;intmaxXor=0;for(intbit=14;bit>=0;bit--){intcurrentBit=(value>>bit)&1;intoppositeBit=1-currentBit;if(current.children[oppositeBit]!=null&¤t.children[oppositeBit].count>0){maxXor|=(1<<bit);current=current.children[oppositeBit];}else{current=current.children[currentBit];}}returnmaxXor;}publicintmaxXor(int[]nums,intlimit){intlength=nums.length;// Prefix XOR arrayint[]prefixXor=newint[length+1];for(inti=0;i<length;i++){prefixXor[i+1]=prefixXor[i]^nums[i];}// Monotonic queues to maintain max and min in sliding windowDeque<Integer>maxDeque=newArrayDeque<>();Deque<Integer>minDeque=newArrayDeque<>();intleft=0;intresult=0;updateTrie(prefixXor[0],1);for(intright=0;right<length;right++){// Maintain decreasing deque for maximumwhile(!maxDeque.isEmpty()&&nums[maxDeque.peekLast()]<=nums[right]){maxDeque.pollLast();}// Maintain increasing deque for minimumwhile(!minDeque.isEmpty()&&nums[minDeque.peekLast()]>=nums[right]){minDeque.pollLast();}maxDeque.addLast(right);minDeque.addLast(right);// Shrink window if max - min exceeds limitwhile(nums[maxDeque.peekFirst()]-nums[minDeque.peekFirst()]>limit){if(maxDeque.peekFirst()==left){maxDeque.pollFirst();}if(minDeque.peekFirst()==left){minDeque.pollFirst();}updateTrie(prefixXor[left],-1);left++;}result=Math.max(result,getMaxXor(prefixXor[right+1]));updateTrie(prefixXor[right+1],1);}returnresult;}}