Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 754 Bytes

D-16submission.md

File metadata and controls

28 lines (25 loc) · 754 Bytes

👺 Question-Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum result of ai XOR aj, where 0 ≤ i, j < n. Could you do this in O(n) runtime? Example: Input: [3, 10, 5, 25, 2, 8] Output: 28 Explanation: The maximum result is 5 ^ 25 = 28.

🍱 Solution

class Solution {
    public int findMaximumXOR(int[] nums) {
        if(nums.length==0 || nums==null)
            return 0;
        Arrays.sort(nums);
        int i,j,maxxor=0;
        for(i=0;i<nums.length;i++){
            for(j=i+1;j<nums.length;j++){
                maxxor=Math.max((nums[i]^nums[j]),maxxor);
            }
        }
        return maxxor;
    }
}