Given an array arr[] of N positive integers the task is to find the minimum value of Bitwise XOR of Bitwise OR and AND of any pair in the given array.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 1
Explanation:
For element 2 & 3:
The value of the expression (2&3) xor (2|3) is 1, which is the minimum from all the pairs in the given array.Input : arr[] = {3, 6, 8, 4, 5}
Output : 1
Explanation:
For element 4 & 5:
The value of the expression (4&5) xor (4|5) is 1, which is the minimum from all the pairs in the given array.
Approach: For any pairs of elements say X and Y, the value of the expression (X&Y) xor (X|Y) can be written as:
=> (X.Y)^(X+Y)
=> (X.Y)(X+Y)’ + (X.Y)'(X+Y)
=> (X.Y)(X’.Y’) + (X’+Y’)(X+Y)
=> X.X’.Y.Y’ + X’.X + X’.Y + Y’.X + Y’.Y
=> 0 + 0 + X’.Y + Y’.X + 0
=> X^Y
From the above calculations, for any pairs (X, Y) in the given array, the expression (X&Y) xor (X|Y) is reduced to X xor Y. Therefore, the minimum value of Bitwise XOR of Bitwise OR and AND of any pair in the given array is given XOR of minimum value pair which can be calculated using the approach discussed in this article.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <algorithm> #include <climits> #include <iostream> using namespace std; // Function to find the minimum // value of XOR of AND and OR of // any pair in the given array int maxAndXor(int arr[], int n) { int ans = INT_MAX; // Sort the array sort(arr, arr + n); // Traverse the array arr[] for (int i = 0; i < n - 1; i++) { // Compare and Find the minimum // XOR value of an array. ans = min(ans, arr[i] ^ arr[i + 1]); } // Return the final answer return ans; } // Driver Code int main() { // Given array int arr[N] = { 1, 2, 3, 4, 5 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call cout << maxAndXor(arr, N); return 0; } |
Java
// Java program to for above approach import java.io.*; import java.util.Arrays; class GFG{ // Function to find the minimum // value of XOR of AND and OR of // any pair in the given array static int maxAndXor(int arr[], int n) { int ans = 9999999; // Sort the array Arrays.sort(arr); // Traverse the array arr[] for (int i = 0; i < n - 1; i++) { // Compare and Find the minimum // XOR value of an array. ans = Math.min(ans, arr[i] ^ arr[i + 1]); } // Return the final answer return ans; } // Driver Code public static void main (String[] args) { // Given array arr[] int arr[] = new int[]{ 1, 2, 3, 4, 5 }; int N = arr.length; // Function Call System.out.println(maxAndXor(arr, N)); } } // This code is contributed by Shubham Prakash |
Python3
# Python3 program for the above approach # Function to find the minimum # value of XOR of AND and OR of # any pair in the given array def maxAndXor(arr, n): ans = float('inf') # Sort the array arr.sort() # Traverse the array arr[] for i in range(n - 1): # Compare and Find the minimum # XOR value of an array. ans = min(ans, arr[i] ^ arr[i + 1]) # Return the final answer return ans # Driver Code if __name__ == '__main__': # Given array arr = [ 1, 2, 3, 4, 5 ] N = len(arr) # Function Call print(maxAndXor(arr, N)) # This code is contributed by Shivam Singh |
C#
// C# program to for above approach using System; class GFG{ // Function to find the minimum // value of XOR of AND and OR of // any pair in the given array static int maxAndXor(int []arr, int n) { int ans = 9999999; // Sort the array Array.Sort(arr); // Traverse the array arr[] for (int i = 0; i < n - 1; i++) { // Compare and Find the minimum // XOR value of an array. ans = Math.Min(ans, arr[i] ^ arr[i + 1]); } // Return the final answer return ans; } // Driver Code public static void Main() { // Given array arr[] int []arr = new int[]{ 1, 2, 3, 4, 5 }; int N = arr.Length; // Function Call Console.Write(maxAndXor(arr, N)); } } // This code is contributed by Code_Mech |
1
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Pair with given product | Set 1 (Find if any pair exists)
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum distance between any special pair in the given array
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Minimum XOR Value Pair
- XOR of Sum of every possible pair of an array
- Quadruplet pair with XOR zero in the given Array
- Print all possible pair with prime XOR in the Array
- Minimize count of array elements to be removed to maximize difference between any pair up to K
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Store duplicate keys-values pair and sort the key-value pair by key
- Find XOR of two number without using XOR operator
- Given a set, find XOR of the XOR's of all subsets.
- Choose X such that (A xor X) + (B xor X) is minimized
- Maximum XOR value of a pair from a range
- Pair formation such that maximum pair sum is minimized
- Probability of a random pair being the maximum weighted pair
- XOR of path between any two nodes in a Binary Tree
- Print all possible palindromic string formed using any pair of given strings
- Count of elements which cannot form any pair whose sum is power of 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

