Maximum XOR Value in JavaTwo arrays containing non-negative numbers are given to us as input. Our task is to find the maximum value of p ^ q, where p is any element from the first array and q is any element from the second array. Along with the maximum value, display the pair whose XOR gives the maximum value. If there is more than one pair that gives the maximum XOR value, then print that pair whose p value comes first in the first input array. Example 1: Input int inArr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Output 30, pair: 10, 30 Explanation: The maximum XOR value for the given input arrays is 30 in all of the possible pairs, and it comes with the elements 10 (from the first array) and 20 (from the second array). Example 2: Input int inArr1[] = {25, 10, 2, 8, 5, 3} Output 28, pair: 25, 5 Explanation: The maximum XOR value for the given input arrays is 28, and it comes with the elements 25 (from the first array) and 5 (from the second array). Note that if we take element five from the first array and element 25 from the second array, we will get the same result. However, element 5 is from the first array and comes after element 25. Hence, it is avoided. Simple Approach: Using Nested For-LoopsUsing the nested for-loops, we can find the maximum value. The outer loop iterates through the first array. For each iteration of the outer loop, traverse through each element of the second array with the help of the inner for-loop. For each iteration of the inner loop, find the XOR value of the elements pointed by the loop variables of the outer and inner for-loops, respectively. Also, compare the current XOR value with the maximum XOR value found till now. Update the maximum XOR value if the current XOR value is more than the maximum XOR value found till now. The elements involved in the maximum XOR value are the pair. FileName: MaxXorVal.java Output: The maximum XOR value is: 30 The pair that gives the max XOR value is: (10, 20) The maximum XOR value is: 28 The pair that gives the max XOR value is: (25, 5) Complexity Analysis: The program uses the nested for-loops, in which the outer loop runs O(m) time, and for each iteration of the outer loop the inner loop runs O(n) times. Therefore, the time complexity of the program is O(m x n), where m is the total number of elements present in the first array and n is the total number of elements present in the second array. The space complexity of the program is O(1), as the program is not using any extra space. Approach: Using TRIEWe can achieve the maximum XOR value by finding those elements whose leftmost bits differ in value. In other words, if the value of the jth bit is 1 for an element 'P' taken from the first array, then the jth bit should be 0 for the element 'Q' taken from the second array and vice-versa. To find those elements, we will start from the most significant bit (the leftmost bit) and will proceed toward the right direction till we find a bit whose value is different in the binary representation of the elements 'P' and 'Q'. In order to efficiently check the value in the first array, we can use a TRIE data structure for every element present in the second array. For every element of the second array, we do the binary conversion and insert every bit into the TRIE. Note that the root will be the most significant bit. Now, we will traverse through every element of the first array and change it into the binary representation and will traverse through all of the bits beginning from the most significant bit. If the current bit is '0', then we move to '1' child in trie if available and vice versa. Finally, we will find a corresponding element from the first array such that its XOR value with the current element of the second array is maximum. In the end, we will return the maximum of all such XOR values found. FileName: MaxXorVal1.java Output: The maximum XOR value is: 30 The pair that gives the max XOR value is: (10, 20) The maximum XOR value is: 28 The pair that gives the max XOR value is: (25, 5) Complexity Analysis: The program uses two loops, one for the first array and the other for the second array. Also, the loops are not nested. For every number in the given arrays, we also run a loop from 32 to 0. Hence, the time complexity of the program is O(32 x (m + n)). Also, the program uses a TRIE data structure, which is used to store 32-bit numbers of the second array. Therefore, the space complexity of the program is O(32 x n), where m is the size of the first array, and n is the size of the second array. |