# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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); ` `    ``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

 `= 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``); ` `    ``echo` `"\nMaximum AND Value = "` `.maxAND(``\$arr``, ``\$n``); ` ` `  `//This code is contributed by mits  ` `?> `

Output:

```Pair = 4 6
Maximum AND value = 4
```

My Personal Notes arrow_drop_up Discovering ways to develop a plane for soaring career goals

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.