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

Source :Sprinklr on Campus Internship



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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the size of the smallest
// subset with the maximum value of Bitwise OR
  
#include <bits/stdc++.h>
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[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
int main()
{
    int arr[] = { 5, 1, 3, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << findMaxOR(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[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()
{
    int[] arr = { 5, 1, 3, 4, 2 };
    int n = arr.Length;
    Console.Write(findMaxOR(arr, n));
}
}
  
// This code is contributed by Ita_c.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the size of  
// the smallest subset with the
// maximum value of Bitwise OR 
  
// Comparator to sort array
// in descending order 
function compare($a, $b
    return ($a > $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[0]; 
  
    $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
?>

chevron_right


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.