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++
// 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
// 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
# 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#
// 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 // 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 ?> |
Output:
Pair = 4 6 Maximum AND value = 4
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:
- Maximum AND value of a pair in an array
- Probability of a random pair being the maximum weighted pair
- Maximum OR value of a pair in an Array | Set 2
- Maximum OR value of a pair in an Array without using OR operator
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum XOR value of a pair from a range
- Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same
- Find a pair with maximum product in array of Integers
- Probability of choosing a random pair with maximum sum in an array
- Find the maximum sum pair in an Array with even parity
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Pair with given sum and maximum shortest distance from end
- Maximum Bitwise AND pair from given range
- Minimum XOR of OR and AND of any pair in the Array
- Number of times Maximum and minimum value updated during traversal of array
- Count ways of choosing a pair with maximum difference
- Find pair with maximum difference in any column of a Matrix
- Maximum Bitwise OR pair from a range
- Maximum possible GCD for a pair of integers with product N
- Given a sorted and rotated array, find if there is a pair with a given sum
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.

