Minimum XOR Value Pair in Java10 Sept 2024 | 8 min read Given an array containing non-negative numbers, our task is to find out a value that represents the minimum XOR value of two numbers from the given array. Consider the following examples. Example 1: Input: int a[] = {10, 8, 5, 3, 1}; Output: 2 Explanation: In the given array, we can make 10 pairs (10, 8) = 10 ^ 8 = 2 (10, 5) = 10 ^ 5 = 15 (10, 3) = 10 ^ 3 = 9 (10, 1) = 10 ^ 1 = 11 (8, 5) = 8 ^ 5 = 13 (8, 3) = 8 ^ 3 = 11 (8, 1) = 8 ^ 1 = 9 (5, 3) = 5 ^ 3 = 6 (5, 1) = 5 ^ 1 = 4 (3, 1) = 3 ^ 1 = 2 We see that the minimum XOR value of all the above pairs is 2. Example 2: Input: int a[] = {3, 5, 7, 2}; Output: 1 Explanation: In the given array, we can make 6 pairs. (3, 5) = 3 ^ 5 = 6 (3, 7) = 3 ^ 7 = 4 (3, 2) = 3 ^ 2 = 1 (5, 7) = 5 ^ 7 = 2 (5, 2) = 5 ^ 2 = 7 (7, 2) = 7 ^ 2 = 5 We see that the minimum XOR value of all the above pairs is 1. Naive ApproachThe approach is straightforward, find out every pair of the elements present in the given array. Compute the XOR for every pair and find out the minimum XOR value. Let's see it happening in the following program. FileName: MinXORVal.java Output: For the array: 10 8 5 3 1 The minimum XOR value is: 2 For the array: 3 5 7 2 The minimum XOR value is: 1 Time Complexity: In the above program, we have used two-degree nesting of the for-loops in the method findMinXorVal(). Since for each iteration of the outer loop, the inner loop has a time complexity of O(n). Therefore, the overall time complexity of the program is O(n2), where n is the total number of elements present in the input array. Using SortingIn this approach, we will sort the input array and will find the minimum XOR value using a single loop. The sorting approach works because the XOR of two numbers that are nearer to each other has a lower XOR value as compared to the numbers that are distant. For example: 1 ^ 8 = 9 & 4 ^ 5 = 1. It is because the gap between 4 & 5 is 1 and 1 & 8 is 7. Therefore, when we do sorting, the numbers that are nearer to one another come one after the other, and then all we have to do is to run a single loop and find XOR of consecutive elements. Observe the following program. FileName: MinXORVal1.java Output: For the array: 10 8 5 3 1 The minimum XOR value is: 2 For the array: 3 5 7 2 The minimum XOR value is: 1 Time Complexity: In the above program, we have used sorting and a single for-loop. Sorting takes O(nlog(n)) time, and the loop takes O(n) time. Therefore, the overall time complexity of the above program is O(n + nlog(n)), where n is the total number of elements present in the input array. Note that this approach is better than the previous one. Using TRIEUsing TRIE, we will get the most optimized solution as we can solve the problem in even lesser time than O(nlog(n)). Let's see the algorithm of it. Step 1: Make an empty TRIE such that each node of the TRIE has two children, one for the 0 and the other for the 1 bit. Step 2: Do the Initialization of minXorVal as INT_MAX, and insert a[0] into TRIE. Step 3: Traverse all the elements of the array one by one, beginning from the second element, and do the following:
Step 4: return the minXorVal FileName: MinXORVal2.java Output: For the array: 10 8 5 3 1 The minimum XOR value is: 2 For the array: 3 5 7 2 The minimum XOR value is: 1 Time Complexity: The time complexity of the above program is O(n), where n is the total number of elements present in the array. Next TopicIccanobif Numbers in Java |
Java OCR
What is Tesseract OCR? The Tesseract OCR is an optical character reading engine developed by HP laboratories in 1985 and launched in 2005. Since 2006 it has been developed by Google. Tesseract has Unicode Support (UTF-8) and can detect more than 100 languages "out of the box"...
6 min read
Middle Digit Number in Java
In this section, we will learn what is middle digit number and also create Java programs to find the middle digit number. It is frequently asked in Java coding tests and academics. Middle Digit Number The middle digit of a number is that which is exactly in-mid of...
2 min read
Check if a given string is Even-Odd Palindrome or not in Java
Determining if a given string is an even-odd palindrome is the task at sight for a given string str. When the characters at even indices make a palindrome, and the characters at odd indices form a palindrome independently, the string is said to be an...
5 min read
String Templates in Java 21
Java 21, the latest release of the Java programming language, brings several exciting features and enhancements to the table. One of these notable features is the introduction of string templates, which simplify string formatting and interpolation. In this section, we'll delve into the world of string...
3 min read
Nested Loops in Java
The English meaning of nested is inside another. It means nested loops are the loop statement inside another loop statement. In simple terms, the loop within a loop is called a nested loop. The inner loop runs fully before the outer loop moves to the ...
6 min read
EvalEx Java: Expression Evaluation in Java
In Java, evaluating mathematical expressions can sometimes be a complex and error-prone task. Manually parsing and calculating expressions can lead to tedious and lengthy code. To simplify this process, we can leverage the power of EvalEx (Evaluate Expression) Java, a lightweight Java library that provides an...
5 min read
String Sort Custom in Java
Two strings a1 and a2 are given to us. All characters of the string a1 are unique and are sorted in a particular order. Our job is to permute the characters of the string a2, such that the order in which the characters are coming in...
6 min read
ClassNotFoundException Java
Java is a popular programming language used by developers around the world to build a wide range of applications. Despite its popularity and reliability, Java programs are prone to errors and exceptions. One of the most common exceptions in Java is the ClassNotFoundException. In this article,...
4 min read
Advantages of Lambda Expression in Java 8
Java 8 introduced a number of new features to the programming language, one of the most significant being the lambda expression. Lambda expressions provide a concise way to express a method that can be passed around as an argument to another method, enabling functional programming paradigms...
4 min read
Core Java MCQ
1. Which of the following is a marker interface? Serializable Cloneable Remote All of the above Show AnswerWorkspace Answer: d) Explanation: Marker interfaces in Java are empty interfaces used to signal to the JVM or other code that objects of the implementing class should be treated differently. Examples include Serializable, Cloneable, and...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India