Remove minimum numbers from the array to get minimum OR value

03 Mar, 2022
Given an array arr[] of N positive integers, the task is to find the minimum number of elements to be deleted from the array so that the bitwise OR of the array elements get minimized. You are not allowed to remove all the elements i.e. at least one element must remain in the array.Examples:  
Input: arr[] = {1, 2, 3} Output: 2 All possible subsets and there OR values are: a) {1, 2, 3} = 3 b) {1, 2} = 3 c) {2, 3} = 3 d) {1, 3} = 3 e) {1} = 1 f) {2} = 2 g) {3} = 3 The minimum possible OR will be 1 from the subset {1}. So, we will need to remove 2 elements.Input: arr[] = {3, 3, 3} Output: 0  
 
Naive approach: Generate all possible sub-sequences and test which one gives the minimum ‘OR’ value. Let the length of the largest sub-sequence with minimum possible OR be L then the answer will N – L. This will take exponential time.Better approach: The minimum value will always be equal to the smallest number present in the array. If this number get bitwise ORed with any other number other than itself then the value of the OR will change and it won’t stay minimum anymore. Thus, we need to remove all the elements that are not equal to this minimum element.  
Find the smallest number in the array.
Find the frequency of this element in the array say cnt.
The final answer will be N – cnt.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the minimum// deletions to get minimum ORint findMinDel(int* arr, int n){     // To store the minimum element    int min_num = INT_MAX;     // Find the minimum element    // from the array    for (int i = 0; i < n; i++)        min_num = min(arr[i], min_num);     // To store the frequency of    // the minimum element    int cnt = 0;     // Find the frequency of the    // minimum element    for (int i = 0; i < n; i++)        if (arr[i] == min_num)            cnt++;     // Return the final answer    return n - cnt;} // Driver codeint main(){    int arr[] = { 3, 3, 2 };    int n = sizeof(arr) / sizeof(int);     cout << findMinDel(arr, n);     return 0;}
// Java implementation of the approachclass GFG{ // Function to return the minimum// deletions to get minimum ORstatic int findMinDel(int []arr, int n){     // To store the minimum element    int min_num = Integer.MAX_VALUE;     // Find the minimum element    // from the array    for (int i = 0; i < n; i++)        min_num = Math.min(arr[i], min_num);     // To store the frequency of    // the minimum element    int cnt = 0;     // Find the frequency of the    // minimum element    for (int i = 0; i < n; i++)        if (arr[i] == min_num)            cnt++;     // Return the final answer    return n - cnt;} // Driver codepublic static void main(String[] args){    int arr[] = { 3, 3, 2 };    int n = arr.length;     System.out.print(findMinDel(arr, n));}} // This code is contributed by PrinciRaj1992
# Python3 implementation of the approachimport sys # Function to return the minimum# deletions to get minimum ORdef findMinDel(arr, n) :     # To store the minimum element    min_num = sys.maxsize;     # Find the minimum element    # from the array    for i in range(n) :        min_num = min(arr[i], min_num);     # To store the frequency of    # the minimum element    cnt = 0;     # Find the frequency of the    # minimum element    for i in range(n) :        if (arr[i] == min_num) :            cnt += 1;     # Return the final answer    return n - cnt; # Driver codeif __name__ == "__main__" :     arr = [ 3, 3, 2 ];    n = len(arr);         print(findMinDel(arr, n)); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{ // Function to return the minimum// deletions to get minimum ORstatic int findMinDel(int []arr, int n){     // To store the minimum element    int min_num = int.MaxValue;     // Find the minimum element    // from the array    for (int i = 0; i < n; i++)        min_num = Math.Min(arr[i],                           min_num);     // To store the frequency of    // the minimum element    int cnt = 0;     // Find the frequency of the    // minimum element    for (int i = 0; i < n; i++)        if (arr[i] == min_num)            cnt++;     // Return the readonly answer    return n - cnt;} // Driver codepublic static void Main(String[] args){    int []arr = { 3, 3, 2 };    int n = arr.Length;     Console.Write(findMinDel(arr, n));}} // This code is contributed by 29AjayKumar
<script> // Javascript implementation of the approach // Function to return the minimum// deletions to get minimum ORfunction findMinDel(arr, n){     // To store the minimum element    var min_num = 1000000000;     // Find the minimum element    // from the array    for (var i = 0; i < n; i++)        min_num = Math.min(arr[i], min_num);     // To store the frequency of    // the minimum element    var cnt = 0;     // Find the frequency of the    // minimum element    for (var i = 0; i < n; i++)        if (arr[i] == min_num)            cnt++;     // Return the final answer    return n - cnt;} // Driver codevar arr = [3, 3, 2];var n = arr.length;document.write( findMinDel(arr, n)); // This code is contributed by noob2000.</script>
2
 
Time Complexity: O(N)
Auxiliary Space: O(1) 
ankthon
princiraj1992
29AjayKumar
noob2000
subhammahato348
Bitwise-OR
Algorithms
Arrays
Mathematical
Arrays
Mathematical
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
What is Hashing | A Complete Tutorial
Find if there is a path between two vertices in an undirected graph
How to Start Learning DSA?
Complete Roadmap To Learn DSA From Scratch
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Arrays in Java
Write a program to reverse an array or string
Maximum and minimum of an array using minimum number of comparisons
Largest Sum Contiguous Subarray
Arrays in C/C++