Print pair with maximum AND value in an array
Last Updated :
15 Jun, 2022
Given an array of n positive elements, find the maximum AND value and the pair of elements generating the maximum AND value from the array.
AND is bitwise & operator.
Examples:
Input : arr[] = {4, 8, 12, 16}
Output : Pair = 8, 12
Maximum AND value = 8
Input : arr[] = {4, 8, 16, 2}
Output : Pair = Not Possible
Maximum AND value = 0
Approach:
Finding Maximum AND value is same as Maximum AND value in an array. Our task is to find the pair of elements resulting in obtained AND value. For finding the elements, simply traverse the whole array and find the AND value of each element with the obtained maximum AND value (result) and if arr[i] & result == result , that means arr[i] is the element which will generate maximum AND value. Also, in the case if maximum AND value (result) is zero then we should print “Not possible” in that case.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int checkBit( int pattern, int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
int maxAND( int arr[], int n)
{
int res = 0, count;
for ( int bit = 31; bit >= 0; bit--) {
count = checkBit(res | (1 << bit), arr, n);
if (count >= 2)
res |= (1 << bit);
}
if (res == 0)
cout << "Not Possible\n" ;
else {
cout << "Pair = " ;
count = 0;
for ( int i = 0; i < n && count < 2; i++) {
if ((arr[i] & res) == res) {
count++;
cout << arr[i] << " " ;
}
}
}
return res;
}
int main()
{
int arr[] = { 4, 8, 6, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "\nMaximum AND Value = "
<< maxAND(arr, n);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG
{
static int checkBit( int pattern, int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
static int maxAND( int arr[], int n)
{
int res = 0 , count;
for ( int bit = 31 ; bit >= 0 ; bit--) {
count = checkBit(res | ( 1 << bit), arr, n);
if (count >= 2 )
res |= ( 1 << bit);
}
if (res == 0 )
System.out.println( "Not Possible" );
else {
System.out.print( "Pair = " );
count = 0 ;
for ( int i = 0 ; i < n && count < 2 ; i++) {
if ((arr[i] & res) == res) {
count++;
System.out.print(arr[i] + " " );
}
}
System.out.println();
}
return res;
}
public static void main(String args[])
{
int arr[] = { 4 , 8 , 6 , 2 };
int n = arr.length;
System.out.println( "Maximum AND Value = "
+ maxAND(arr, n));
}
}
|
Python3
def checkBit(pattern, arr, n):
count = 0
for i in range ( 0 , n):
if ((pattern & arr[i]) = = pattern):
count + = 1
return count
def maxAND(arr, n):
res = 0
for bit in range ( 31 , - 1 , - 1 ) :
count = checkBit(res | ( 1 << bit),
arr, n)
if (count > = 2 ):
res | = ( 1 << bit)
if (res = = 0 ):
print ( "Not Possible" )
else :
print ( "Pair = " , end = "")
count = 0
i = 0
while (i < n and count < 2 ):
if ((arr[i] & res) = = res) :
count + = 1
print (arr[i] , end = " " )
i + = 1
return res
arr = [ 4 , 8 , 6 , 2 ]
n = len (arr)
print ( "\nMaximum AND Value = " ,
maxAND(arr, n))
|
C#
using System;
class GFG
{
static int checkBit( int pattern, int []arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
static int maxAND( int []arr, int n)
{
int res = 0, count;
for ( int bit = 31; bit >= 0; bit--) {
count = checkBit(res | (1 << bit), arr, n);
if (count >= 2)
res |= (1 << bit);
}
if (res == 0)
Console.Write( "Not Possible" );
else {
Console.Write( "Pair = " );
count = 0;
for ( int i = 0; i < n && count < 2; i++)
{
if ((arr[i] & res) == res) {
count++;
Console.Write(arr[i] + " " );
}
}
Console.WriteLine();
}
return res;
}
public static void Main()
{
int []arr = { 4, 8, 6, 2 };
int n = arr.Length;
Console.WriteLine( "Maximum AND Value = "
+ maxAND(arr, n));
}
}
|
PHP
<?php
function checkBit( $pattern , $arr , $n )
{
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
if (( $pattern & $arr [ $i ]) == $pattern )
$count ++;
return $count ;
}
function maxAND( $arr , $n )
{
$res = 0;
for ( $bit = 31; $bit >= 0; $bit --)
{
$count = checkBit( $res | (1 << $bit ),
$arr , $n );
if ( $count >= 2)
$res |= (1 << $bit );
}
if ( $res == 0)
echo "Not Possible\n" ;
else {
echo "Pair = " ;
$count = 0;
for ( $i = 0; $i < $n &&
$count < 2; $i ++)
{
if (( $arr [ $i ] & $res ) == $res )
{
$count ++;
echo $arr [ $i ]. " " ;
}
}
}
return $res ;
}
$arr = array ( 4, 8, 6, 2 );
$n = sizeof( $arr ) / sizeof( $arr [0]);
echo "\nMaximum AND Value = " .maxAND( $arr , $n );
?>
|
Javascript
<script>
function checkBit(pattern, arr, n)
{
let count = 0;
for (let i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
function maxAND(arr, n)
{
let res = 0, count;
for (let bit = 31; bit >= 0; bit--) {
count = checkBit(res | (1 << bit), arr, n);
if (count >= 2)
res |= (1 << bit);
}
if (res == 0)
System.out.prletln( "Not Possible" );
else {
document.write( "Pair = " );
count = 0;
for (let i = 0; i < n && count < 2; i++) {
if ((arr[i] & res) == res) {
count++;
document.write(arr[i] + " " );
}
}
document.write( "<br/>" );
}
return res;
}
let arr = [ 4, 8, 6, 2 ];
let n = arr.length;
document.write( "Maximum AND Value = "
+ maxAND(arr, n));
</script>
|
Output:
Pair = 4 6
Maximum AND value = 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...