Bitwise AND of all unordered pairs from a given array
Given an array arr[] of size N, the task is to find the bitwise AND of all possible unordered pairs present in the given array.
Examples:
Input: arr[] = {1, 5, 3, 7}
Output: 1
Explanation:
All possible unordered pairs are (1, 5), (1, 3), (1, 7), (5, 3), (5, 7), (3, 7).
Bitwise AND of all possible pairs = ( 1 & 5 ) & ( 1 & 3 ) & ( 1 & 7 ) & ( 5 & 3 ) & ( 5 & 7 ) & ( 3 & 7 )
= 1 & 1 & 1 & 1 & 5 & 3
= 1
Therefore, the required output is 1.
Input: arr[] = {4, 5, 12, 15}
Output: 4
Naive approach: The idea is to traverse the array and generate all possible pairs of the given array. Finally, print Bitwise AND of each element present in these pairs of the given array. Follow the steps below to solve the problem:
- Initialize a variable, say totalAND, to store Bitwise AND of each element from these pairs.
- Iterate over the array and generate all possible pairs (arr[i], arr[j]) from the given array.
- For each pair (arr[i], arr[j]), update the value of totalAND = (totalAND & arr[i] & arr[j]).
- Finally, print the value of totalAND.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int TotalAndPair( int arr[], int N)
{
int totalAND = (1 << 30) - 1;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N;
j++) {
totalAND &= arr[i]
& arr[j];
}
}
return totalAND;
}
int main()
{
int arr[] = { 4, 5, 12, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << TotalAndPair(arr, N);
}
|
Java
import java.util.*;
class GFG{
static int TotalAndPair( int arr[], int N)
{
int totalAND = ( 1 << 30 ) - 1 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N;
j++)
{
totalAND &= arr[i]
& arr[j];
}
}
return totalAND;
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 12 , 15 };
int N = arr.length;
System.out.print(TotalAndPair(arr, N));
}
}
|
Python3
def TotalAndPair(arr, N):
totalAND = ( 1 << 30 ) - 1
for i in range (N):
for j in range (i + 1 , N):
totalAND & = (arr[i] & arr[j])
return totalAND
if __name__ = = '__main__' :
arr = [ 4 , 5 , 12 , 15 ]
N = len (arr)
print (TotalAndPair(arr, N))
|
C#
using System;
class GFG{
static int TotalAndPair( int [] arr, int N)
{
int totalAND = (1 << 30) - 1;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
totalAND &= arr[i] & arr[j];
}
}
return totalAND;
}
public static void Main()
{
int [] arr = { 4, 5, 12, 15 };
int N = arr.Length;
Console.Write(TotalAndPair(arr, N));
}
}
|
Javascript
<script>
function TotalAndPair(arr, N)
{
let totalAND = (1 << 30) - 1;
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N;
j++) {
totalAND &= arr[i]
& arr[j];
}
}
return totalAND;
}
let arr = [ 4, 5, 12, 15 ];
let N = arr.length;
document.write(TotalAndPair(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
- Considering an array of 4 elements, required Bitwise AND is as follows:
(arr[0] & arr[1]) & (arr[0] & arr[2]) & (arr[0] & arr[3]) & (arr[1] & arr[2]) & (arr[1] & arr[3]) & (arr[2] & arr[3])
(arr[0] & arr[0] & arr[0]) & (arr[1] & arr[1] & arr[1]) & (arr[2] & arr[2] & arr[2]) & (arr[3] & arr[3] & arr[3])
- It can be observed that each array element occurs exactly (N – 1) times in all possible pairs.
- Based on the X & X = X property of Bitwise AND operators, the above expression can be rearranged to arr[0] & arr[1] & arr[2] & arr[3], which is equal to the Bitwise AND of all elements of original array.
Follow the steps below to solve the problem:
- Initialize a variable totalAND to store the result.
- Traverse the given array.
- Calculate Bitwise AND of all unordered pairs by updating totalAND = totalAND & arr[i].
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int TotalAndPair( int arr[], int N)
{
int totalAND = (1 << 30) - 1;
for ( int i = 0; i < N; i++) {
totalAND &= arr[i];
}
return totalAND;
}
int main()
{
int arr[] = { 4, 5, 12, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << TotalAndPair(arr, N);
}
|
Java
import java.util.*;
class GFG{
static int TotalAndPair( int arr[], int N)
{
int totalAND = ( 1 << 30 ) - 1 ;
for ( int i = 0 ; i < N; i++) {
totalAND &= arr[i];
}
return totalAND;
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 12 , 15 };
int N = arr.length;
System.out.print(TotalAndPair(arr, N));
}
}
|
Python3
def TotalAndPair(arr, N):
totalAND = ( 1 << 30 ) - 1 ;
for i in range (N):
totalAND & = arr[i];
return totalAND;
if __name__ = = '__main__' :
arr = [ 4 , 5 , 12 , 15 ];
N = len (arr);
print (TotalAndPair(arr, N));
|
C#
using System;
class GFG{
static int TotalAndPair( int []arr, int N)
{
int totalAND = (1 << 30) - 1;
for ( int i = 0; i < N; i++)
{
totalAND &= arr[i];
}
return totalAND;
}
public static void Main(String[] args)
{
int []arr = { 4, 5, 12, 15 };
int N = arr.Length;
Console.Write(TotalAndPair(arr, N));
}
}
|
Javascript
<script>
function TotalAndPair(arr, N)
{
let totalAND = (1 << 30) - 1;
for (let i = 0; i < N; i++) {
totalAND &= arr[i];
}
return totalAND;
}
let arr = [ 4, 5, 12, 15 ];
let N = arr.length;
document.write(TotalAndPair(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
26 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...