Java code for the problem using Trie data Structure.


  • 0
    L

    '''import java.util.*; class Solution { public int findMaximumXOR(int[] nums) { if(nums == null || nums.length == 0) { return 0; } // Init Trie. Object[] root = {null, null}; for(int num: nums) { Object[] curNode = root; for(int i = 31; i >= 0; i --) { int curBit = (num >>> i) & 1; if(curNode[curBit] == null) { curNode[curBit] = new Object[]{null, null}; } curNode = (Object[]) curNode[curBit]; } } int max = Integer.MIN_VALUE; for(int num: nums) { Object[] curNode = root; int curSum = 0; for(int i = 31; i >= 0; i --) { int curBit = (num >>> i) & 1; if(curNode[curBit ^ 1] != null) { curSum += (1 << i); curNode = (Object[]) curNode[curBit ^ 1]; }else { curNode = (Object[]) curNode[curBit]; } } //System.out.println(curSum); max = Math.max(curSum, max); } return max; } } class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++) { int n = sc.nextInt(); int arr[] = new int[n]; for(int j=0;j<n;j++) { arr[j]=sc.nextInt(); } Solution s = new Solution(); System.out.println(s.findMaximumXOR(arr)); } } }'''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.