Print pair with maximum AND value in an array
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:
// CPP Program to find pair with
// maximum AND value
#include <bits/stdc++.h>
using namespace std;
// Utility function to check number of
// elements having set msb as of pattern
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;
}
// Function for finding maximum and
// value pair
int maxAND(int arr[], int n)
{
int res = 0, count;
// iterate over total of 30bits
// from msb to lsb
for (int bit = 31; bit >= 0; bit--) {
// find the count of element
// having set msb
count = checkBit(res | (1 << bit), arr, n);
// if count >= 2 set particular
// bit in result
if (count >= 2)
res |= (1 << bit);
}
// Find the elements
if (res == 0)
cout << "Not Possible\n";
else {
// print the pair of elements
cout << "Pair = ";
count = 0;
for (int i = 0; i < n && count < 2; i++) {
// inc count value after
// printing element
if ((arr[i] & res) == res) {
count++;
cout << arr[i] << " ";
}
}
}
// return the result value
return res;
}
// Driver function
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 Program to find pair
// with maximum AND value
import java.util.*;
import java.io.*;
class GFG
{
// Utility function to check number of
// elements having set msb as of pattern
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;
}
// Function for finding maximum and
// value pair
static int maxAND(int arr[], int n)
{
int res = 0, count;
// iterate over total of 30bits
// from msb to lsb
for (int bit = 31; bit >= 0; bit--) {
// find the count of element
// having set msb
count = checkBit(res | (1 << bit), arr, n);
// if count >= 2 set particular
// bit in result
if (count >= 2)
res |= (1 << bit);
}
// Find the elements
if (res == 0)
System.out.println("Not Possible");
else {
// print the pair of elements
System.out.print("Pair = ");
count = 0;
for (int i = 0; i < n && count < 2; i++) {
// inc count value after
// printing element
if ((arr[i] & res) == res) {
count++;
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
// return the result value
return res;
}
// Driver code
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));
}
}
// This code is contributed by Sahil_Bansall
# Python 3 Program to find pair with
# maximum AND value
# Utility function to check number of
# elements having set msb as of pattern
def checkBit(pattern, arr, n):
count = 0
for i in range(0, n):
if ((pattern & arr[i]) == pattern):
count += 1
return count
# Function for finding maximum and
# value pair
def maxAND(arr, n):
res = 0
# iterate over total of 30bits
# from msb to lsb
for bit in range(31 ,-1 ,-1) :
# find the count of element
# having set msb
count = checkBit(res | (1 << bit),
arr, n)
# if count >= 2 set particular
# bit in result
if (count >= 2):
res |= (1 << bit)
# Find the elements
if (res == 0):
print("Not Possible")
else:
# print the pair of elements
print("Pair = ", end = "")
count = 0
i = 0
while(i < n and count < 2):
# inc count value after
# printing element
if ((arr[i] & res) == res) :
count+=1
print(arr[i] , end = " ")
i += 1
# return the result value
return res
# Driver function
arr = [4, 8, 6, 2 ]
n = len(arr)
print("\nMaximum AND Value = ",
maxAND(arr, n))
# This code is contributed by Smitha
// C# Program to find pair
// with maximum AND value
using System;
class GFG
{
// Utility function to check number of
// elements having set msb as of pattern
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;
}
// Function for finding maximum and
// value pair
static int maxAND(int []arr, int n)
{
int res = 0, count;
// iterate over total of 30bits
// from msb to lsb
for (int bit = 31; bit >= 0; bit--) {
// find the count of element
// having set msb
count = checkBit(res | (1 << bit), arr, n);
// if count >= 2 set particular
// bit in result
if (count >= 2)
res |= (1 << bit);
}
// Find the elements
if (res == 0)
Console.Write("Not Possible");
else {
// print the pair of elements
Console.Write("Pair = ");
count = 0;
for (int i = 0; i < n && count < 2; i++)
{
// inc count value after
// printing element
if ((arr[i] & res) == res) {
count++;
Console.Write(arr[i] + " ");
}
}
Console.WriteLine();
}
// return the result value
return res;
}
// Driver code
public static void Main()
{
int []arr = { 4, 8, 6, 2 };
int n = arr.Length;
Console.WriteLine("Maximum AND Value = "
+ maxAND(arr, n));
}
}
// This code is contributed by vt_m
<?php
// php Program to find pair with
// maximum AND value
// Utility function to check number of
// elements having set msb as of pattern
function checkBit($pattern, $arr, $n)
{
$count = 0;
for ($i = 0; $i < $n; $i++)
if (($pattern & $arr[$i]) == $pattern)
$count++;
return $count;
}
// Function for finding maximum
// and value pair
function maxAND($arr, $n)
{
$res = 0;
// iterate over total of 30bits
// from msb to lsb
for ($bit = 31; $bit >= 0; $bit--)
{
// find the count of element
// having set msb
$count = checkBit($res | (1 << $bit),
$arr, $n);
// if count >= 2 set particular
// bit in result
if ($count >= 2)
$res |= (1 << $bit);
}
// Find the elements
if ($res == 0)
echo "Not Possible\n";
else {
// print the pair of elements
echo "Pair = ";
$count = 0;
for ($i = 0; $i < $n &&
$count < 2; $i++)
{
// inc count value after
// printing element
if (($arr[$i] & $res) == $res)
{
$count++;
echo $arr[$i]. " ";
}
}
}
// return the result value
return $res;
}
// Driver code
$arr = array( 4, 8, 6, 2 );
$n = sizeof($arr) / sizeof($arr[0]);
echo "\nMaximum AND Value = " .maxAND($arr, $n);
//This code is contributed by mits
?>
<script>
// Javascript program to find pair
// with maximum AND value
// Utility function to check number of
// elements having set msb as of pattern
function checkBit(pattern, arr, n)
{
let count = 0;
for (let i = 0; i < n; i++)
if ((pattern & arr[i]) == pattern)
count++;
return count;
}
// Function for finding maximum and
// value pair
function maxAND(arr, n)
{
let res = 0, count;
// iterate over total of 30bits
// from msb to lsb
for (let bit = 31; bit >= 0; bit--) {
// find the count of element
// having set msb
count = checkBit(res | (1 << bit), arr, n);
// if count >= 2 set particular
// bit in result
if (count >= 2)
res |= (1 << bit);
}
// Find the elements
if (res == 0)
System.out.prletln("Not Possible");
else {
// print the pair of elements
document.write("Pair = ");
count = 0;
for (let i = 0; i < n && count < 2; i++) {
// inc count value after
// printing element
if ((arr[i] & res) == res) {
count++;
document.write(arr[i] + " ");
}
}
document.write("<br/>");
}
// return the result value
return res;
}
// Driver code
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)