# Size of the smallest subset with maximum Bitwise OR

Given an array of positive integers. The task is to find the size of the smallest subset such that the Bitwise OR of that set is Maximum possible.

Examples:

```Input : arr[] = {5, 1, 3, 4, 2}
Output : 2
7 is the maximum value possible of OR,
5|2 = 7 and 5|3 = 7

Input : arr[] = {2, 6, 2, 8, 4, 5}
Output : 3
15 is the maximum value of OR and set
elements are 8, 6, 5
```

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

Approach : The idea is to observe that, a bit in the result of bitwise OR will be set if it is set in either of the operands. So, to maximize the value of the Bitwise OR:

• We will sort the array in decreasing order.
• Now, take the max element of the array as the maximum value of OR initially.
• Now, traverse the array and for each element calculate its Bitwise OR with maxOR so far. If it is greater than maxOR so far then increment count.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the size of the smallest ` `// subset with the maximum value of Bitwise OR ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Comparator to sort array in descending order ` `bool` `compare(``int` `a, ``int` `b) ` `{ ` `    ``return` `a > b; ` `} ` ` `  `// Function to find the size of the smallest ` `// subset with the maximum value of Bitwise OR ` `int` `findMaxOR(``int` `arr[], ``int` `n) ` `{ ` `    ``// sort the array in decreasing ` `    ``// order ` `    ``sort(arr, arr + n, compare); ` ` `  `    ``// Maximum element in the set ` `    ``int` `maxOR = arr; ` ` `  `    ``int` `count = 1; ` ` `  `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``// if bitwise or with the next element is ` `        ``// greater than current value ` `        ``if` `((maxOR | arr[i]) > maxOR) ` `        ``// then add this element in the set and ` `        ``// update the current maxOR value ` `        ``{ ` `            ``maxOR = maxOR | arr[i]; ` `            ``count++; ` `        ``} ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 5, 1, 3, 4, 2 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << findMaxOR(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find the size of the smallest ` `// subset with the maximum value of Bitwise OR ` `import` `java.util.*; ` ` `  `class` `solution ` `{ ` `     `  `  ``// Comparator to sort array in descending order ` `boolean` `compare(``int` `a, ``int` `b) ` `{ ` `    ``return` `a > b; ` `} ` ` `  `// Function to find the size of the smallest ` `// subset with the maximum value of Bitwise OR ` `static` `int` `findMaxOR(``int` `[]arr, ``int` `n) ` `{ ` `    ``// sort the array in decreasing ` `    ``// order ` `    ``int` `temp; ` `     ``for` `(``int` `i = ``0``; i < n; i++)  ` ` `  `        ``{ ` ` `  `            ``for` `(``int` `j = i + ``1``; j < n; j++)  ` ` `  `            ``{ ` ` `  `                ``if` `(arr[i] < arr[j])  ` ` `  `                ``{ ` ` `  `                    ``temp = arr[i]; ` ` `  `                    ``arr[i] = arr[j]; ` ` `  `                    ``arr[j] = temp; ` ` `  `                ``} ` ` `  `            ``} ` ` `  `        ``} ` `    ``// Maximum element in the set ` `    ``int` `maxOR = arr[``0``]; ` ` `  `    ``int` `count = ``1``; ` ` `  `    ``for` `(``int` `i = ``1``; i < n; i++) { ` `        ``// if bitwise or with the next element is ` `        ``// greater than current value ` `        ``if` `((maxOR | arr[i]) > maxOR) ` `        ``// then add this element in the set and ` `        ``// update the current maxOR value ` `        ``{ ` `            ``maxOR = maxOR | arr[i]; ` `            ``count++; ` `        ``} ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String arrgs[]) ` `{ ` `    ``int` `[]arr = { ``5``, ``1``, ``3``, ``4``, ``2` `}; ` `    ``int` `n = arr.length; ` ` `  `    ``System.out.println(findMaxOR(arr, n)); ` ` `  `} ` `   `  `} ` `// This code is contributed by ` `// Surendra_Gangwar `

## Python3

 `# Python 3 program to find the size of  ` `# the smallest subset with the ` `# maximum value of Bitwise OR  ` ` `  `# Function to find the size of the smallest ` `# subset with the maximum value of Bitwise OR ` `def` `findMaxOR(arr, n) : ` `     `  `    ``# sort the array in decreasing ` `    ``# order ` `    ``arr.sort(reverse ``=` `True``) ` ` `  `    ``# Maximum element in the set ` `    ``maxOR ``=` `arr[``0``] ` ` `  `    ``count ``=` `1` ` `  `    ``for` `i ``in` `range``(``1``, n) : ` `         `  `        ``# if bitwise or with the next element  ` `        ``# is greater than current value ` `        ``if` `((maxOR | arr[i]) > maxOR) : ` `             `  `            ``# then add this element in the set and ` `            ``# update the current maxOR value ` `            ``maxOR ``=` `maxOR | arr[i]; ` `            ``count ``+``=` `1` ` `  `    ``return` `count ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``arr ``=` `[ ``5``, ``1``, ``3``, ``4``, ``2` `] ` `    ``n ``=` `len``(arr) ` ` `  `    ``print``(findMaxOR(arr, n))  ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# program to find the size of the smallest ` `// subset with the maximum value of Bitwise OR ` ` `  `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Comparator to sort array in descending order ` `bool` `compare(``int` `a, ``int` `b) ` `{ ` `    ``return` `a > b; ` `} ` ` `  `// Function to find the size of the smallest ` `// subset with the maximum value of Bitwise OR ` `static` `int` `findMaxOR(``int``[] arr, ``int` `n) ` `{ ` `    ``// sort the array in decreasing ` `    ``// order ` `    ``int` `temp; ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``for` `(``int` `j = i + 1; j < n; j++)  ` `        ``{ ` `            ``if` `(arr[i] < arr[j])  ` `            ``{ ` `                ``temp = arr[i]; ` `                ``arr[i] = arr[j]; ` `                ``arr[j] = temp; ` `            ``} ` `        ``} ` `    ``} ` `     `  `    ``// Maximum element in the set ` `    ``int` `maxOR = arr; ` ` `  `    ``int` `count = 1; ` ` `  `    ``for` `(``int` `i = 1; i < n; i++) ` `    ``{ ` `         `  `        ``// if bitwise or with the next element is ` `        ``// greater than current value ` `        ``if` `((maxOR | arr[i]) > maxOR) ` `        ``{ ` `            ``// then add this element in the set and ` `            ``// update the current maxOR value ` `            ``maxOR = maxOR | arr[i]; ` `            ``count++; ` `        ``} ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int``[] arr = { 5, 1, 3, 4, 2 }; ` `    ``int` `n = arr.Length; ` `    ``Console.Write(findMaxOR(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by Ita_c. `

## PHP

 ` ``\$b``);  ` `}  ` ` `  `// Function to find the size of  ` `// the smallest subset with the  ` `// maximum value of Bitwise OR  ` `function` `findMaxOR( ``\$arr``, ``\$n``)  ` `{  ` `    ``// sort the array in decreasing  ` `    ``// order  ` `    ``sort(``\$arr``);  ` ` `  `    ``// Maximum element in the set  ` `    ``\$maxOR` `= ``\$arr``;  ` ` `  `    ``\$count` `= 1;  ` ` `  `    ``for` `(``\$i` `= 1; ``\$i` `< ``\$n``; ``\$i``++) ` `    ``{  ` `        ``// if bitwise or with the next element  ` `        ``// is greater than current value  ` `        ``if` `((``\$maxOR` `| ``\$arr``[``\$i``]) > ``\$maxOR``)  ` `         `  `        ``// then add this element in the set and  ` `        ``// update the current maxOR value  ` `        ``{  ` `            ``\$maxOR` `= ``\$maxOR` `|| ``\$arr``[``\$i``];  ` `            ``\$count``++;  ` `        ``}  ` `    ``}  ` ` `  `    ``return` `\$count``;  ` `}  ` ` `  `// Driver Code  ` `\$arr` `= ``array` `(5, 1, 3, 4, 2 );  ` `\$n` `= sizeof(``\$arr``);  ` `echo` `findMaxOR(``\$arr``, ``\$n``);  ` ` `  `// This code is contributed  ` `// by Sach_Code ` `?> `

Output:

```2
```

Time Complexity : O(N*logN)
Auxiliary Space : O(1)

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.