Minimum XOR of OR and AND of any pair in the Array
Last Updated :
05 May, 2021
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 <bits/stdc++.h>
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[] = { 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 = Integer.MAX_VALUE;
// 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
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the minimum
// value of XOR of AND and OR of
// any pair in the given array
function maxAndXor(arr, n)
{
let ans = Number.MAX_VALUE;
// Sort the array
arr.sort();
// Traverse the array arr[]
for (let 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
// Given array arr[]
let arr = [ 1, 2, 3, 4, 5 ];
let N = arr.length;
// Function Call
document.write(maxAndXor(arr, N));
// This code is contributed by sanjoy_62.
</script>
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Similar Reads
Maximum and minimum sum of Bitwise XOR of pairs from an array Given an array arr[] of size N, the task is to find the maximum and minimum sum of Bitwise XOR of all pairs from an array by splitting the array into N / 2 pairs. Examples: Input: arr[] = {1, 2, 3, 4}Output: 6 10Explanation:Bitwise XOR of the all possible pair splits are as follows:(1, 2), (3, 4) â
11 min read
Minimum product pair an array of positive Integers Given an array of positive integers. We are required to write a program to print the minimum product of any two numbers of the given array. Examples: Input: 11 8 5 7 5 100Output: 25 Explanation: The minimum product of any two numbers will be 5 * 5 = 25. Input: 198 76 544 123 154 675 Output: 7448Expl
12 min read
Sum of Bitwise AND of sum of pairs and their Bitwise AND from a given array Given an array arr[] consisting of N integers, the task is to find the sum of Bitwise AND of (arr[i] + arr[j]) and Bitwise AND of arr[i] and arr[j] for each pair of elements (arr[i], arr[j]) from the given array. Since the sum can be very large, print it modulo (109 + 7). Examples: Input: arr[] = {8
9 min read
Minimum sum of absolute difference of pairs of two arrays Given two arrays a[] and b[] of equal length n. The task is to pair each element of array a to an element in array b, such that sum S of absolute differences of all the pairs is minimum.Suppose, two elements a[i] and a[j] (i != j) of a are paired with elements b[p] and b[q] of b respectively, then p
7 min read
Minimum value among AND of elements of every subset of an array Given an array of integers, the task is to find the AND of all elements of each subset of the array and print the minimum AND value among all those.Examples: Input: arr[] = {1, 2, 3} Output: 0 AND of all possible subsets (1 & 2) = 0, (1 & 3) = 1, (2 & 3) = 2 and (1 & 2 & 3) = 0.
3 min read
Sum of Bitwise OR of all pairs in a given array Given an array "arr[0..n-1]" of integers. The task is to calculate the sum of Bitwise OR of all pairs, i.e. calculate the sum of "arr[i] | arr[j]" for all the pairs in the given array where i < j. Here '|' is a bitwise OR operator. The expected time complexity is O(n). Examples: Input: arr[] = {5
13 min read