Given an integer array
nums
, return the maximum result of nums[i] XOR nums[j]
, where 0 ≤ i ≤ j < n
.Example 1:
Input: nums = [3,10,5,25,2,8]
Output: 28
Approach
Java
public class MaxXOR {public static void main(String[] args) {int[] nums = { 3, 10, 5, 25, 2, 8 };System.out.println(findMaximumXOR(nums));}static int findMaximumXOR(int[] nums) {return getMax(nums);}// function to add the new datastatic void insert(int x, Node head) {Node curr = head;for (int i = 30; i >= 0; i--) {int val = (x >> i) & 1;// if zero then go to left sideif (val == 0) {// if left is null then// create a new node for leftif (curr.left == null)curr.left = new Node();// move to next of leftcurr = curr.left;}// if value is 1 then go right sideelse {// if right is null then create// a new node for rightif (curr.right == null)curr.right = new Node();// move next to rightcurr = curr.right;}}}// function to find the maximum// value of xor of two numbers in the// arraystatic int getMax(int[] nums) {int n = nums.length;int ans = 0;Node head = new Node();// add all value into the sructurefor (int i = 0; i < n; i++)insert(nums[i], head);for (int i = 0; i < n; i++) {int curr_xor = 0;int m = (int) Math.pow(2, 30);Node curr = head;for (int j = 30; j >= 0; j--) {int val = (nums[i] >> j) & 1;// if 0if (val == 0) {// if right existif (curr.right != null) {curr_xor += m;curr = curr.right;}// else go for leftelsecurr = curr.left;}// if 1else {// if left existif (curr.left != null) {curr_xor += m;curr = curr.left;}// else go for rightelsecurr = curr.right;}m = m / 2;}// uodate answerif (curr_xor > ans)ans = curr_xor;}return ans;}static class Node {Node left;Node right;public Node() {super();}public Node(Node left, Node right) {super();this.left = left;this.right = right;}}}
C++
#include <bits/stdc++.h>using namespace std;//structute of the nodestruct Node{Node *left;Node *right;};//function to add the new datavoid insert(int x, Node *head){Node *curr = head;for (int i = 30; i >= 0; i--){int val = (x >> i) & 1;//if zero then go to left sideif (val == 0){//if left is null then//create a new node for leftif (curr->left == NULL)curr->left = new Node();//move to next of leftcurr = curr->left;}//if value is 1 then go right sideelse{//if right is null then create//a new node for rightif (curr->right == NULL)curr->right = new Node();//move next to rightcurr = curr->right;}}}//function to find the maximum//value of xor of two numbers in the//arrayint getMax(vector<int> &nums){int n = nums.size();int ans = 0;Node *head = new Node();//add all value into the sructurefor (int i = 0; i < n; i++)insert(nums[i], head);for (int i = 0; i < n; i++){long long int curr_xor = 0;long long int m = pow(2, 30);Node *curr = head;for (long long int j = 30; j >= 0; j--){long long int val = (nums[i] >> j) & 1;//if 0if (val == 0){//if right existif (curr->right){curr_xor += m;curr = curr->right;}//else go for leftelsecurr = curr->left;}//if 1else{//if left existif (curr->left){curr_xor += m;curr = curr->left;}//else go for rightelsecurr = curr->right;}m = m / 2;}//uodate answerif (curr_xor > ans)ans = curr_xor;}return ans;}int findMaximumXOR(vector<int> &nums){return getMax(nums);}int main(){vector<int> nums = {3, 10, 5, 25, 2, 8};cout << findMaximumXOR(nums);return 0;}
No comments:
Post a Comment