# Minimum value among AND of elements of every subset of an array

Given an array of integers, the task is to find the AND of all elements of each subset of the array and print the minimum AND value among all those.

Examples:

```Input: arr[] = {1, 2, 3}
Output: 0
AND of all possible subsets
(1 & 2) = 0,
(1 & 3) = 1,
(2 & 3) = 2 and
(1 & 2 & 3) = 0.
Minimum among these is 0.

Input: arr[] = {7, 2}
Output: 2
```

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

Approach: The minimum AND value of any subset of the array will be the AND of all the elements of the array. So, the simplest way is to find AND of all elements of sub-array.

Below is the implementation of the above approach:
Implementation:

## C++

 `// C++ program for the above approach  ` `#include ` `using` `namespace` `std; ` ` `  `void` `minAND(``int` `arr[], ``int` `n)  ` `{  ` ` `  `    ``int` `s = arr[0];  ` ` `  `    ``// Find AND of whole array  ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `    ``{  ` `        ``s = s & arr[i];  ` `    ``}  ` ` `  `    ``// Print the answer  ` `    ``cout << (s) << endl;  ` `}  ` `     `  `// Driver code  ` `int` `main()  ` `{  ` `    ``int` `arr[] = {1, 2, 3};  ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(``int``);  ` `    ``minAND(arr, n);  ` `}  ` ` `  `// This code has been contributed by Arnab Kundu  `

## Java

 `// Java program for the above approach ` `class` `GFG  ` `{ ` ` `  `    ``static` `void` `minAND(``int``[] arr, ``int` `n)  ` `    ``{ ` ` `  `        ``int` `s = arr[``0``]; ` ` `  `        ``// Find AND of whole array ` `        ``for` `(``int` `i = ``1``; i < n; i++)  ` `        ``{ ` `            ``s = s & arr[i]; ` `        ``} ` ` `  `        ``// Print the answer ` `        ``System.out.println(s); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int``[] arr = {``1``, ``2``, ``3``}; ` `        ``int` `n = arr.length; ` `        ``minAND(arr, n); ` `    ``} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

## Python

 `# Python program for the above approach ` `def` `minAND(arr, n): ` `     `  `    ``s ``=` `arr[``0``] ` `     `  `    ``# Find AND of whole array ` `    ``for` `i ``in` `range``(``1``, n): ` `        ``s ``=` `s & arr[i] ` `     `  `    ``# Print the answer ` `    ``print``(s) ` ` `  `# Driver code ` `arr ``=` `[``1``, ``2``, ``3``] ` `n ``=` `len``(arr) ` `minAND(arr, n) `

## C#

 `// C# program for the above approach ` `class` `GFG  ` `{ ` ` `  `    ``static` `void` `minAND(``int``[] arr, ``int` `n)  ` `    ``{ ` ` `  `        ``int` `s = arr[0]; ` ` `  `        ``// Find AND of whole array ` `        ``for` `(``int` `i = 1; i < n; i++)  ` `        ``{ ` `            ``s = s & arr[i]; ` `        ``} ` ` `  `        ``// Print the answer ` `        ``System.Console.WriteLine(s); ` `    ``} ` `     `  `    ``// Driver code ` `    ``static` `void` `Main()  ` `    ``{ ` `        ``int``[] arr = {1, 2, 3}; ` `        ``int` `n = arr.Length; ` `        ``minAND(arr, n); ` `    ``} ` `} ` ` `  `// This code has been contributed by chandan_jnu `

## PHP

 ` `

Output:

```0
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.