Remove minimum numbers from the array to get minimum OR value
Last Updated :
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++
#include <bits/stdc++.h>
using namespace std;
int findMinDel( int * arr, int n)
{
int min_num = INT_MAX;
for ( int i = 0; i < n; i++)
min_num = min(arr[i], min_num);
int cnt = 0;
for ( int i = 0; i < n; i++)
if (arr[i] == min_num)
cnt++;
return n - cnt;
}
int main()
{
int arr[] = { 3, 3, 2 };
int n = sizeof (arr) / sizeof ( int );
cout << findMinDel(arr, n);
return 0;
}
|
Java
class GFG
{
static int findMinDel( int []arr, int n)
{
int min_num = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
min_num = Math.min(arr[i], min_num);
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
if (arr[i] == min_num)
cnt++;
return n - cnt;
}
public static void main(String[] args)
{
int arr[] = { 3 , 3 , 2 };
int n = arr.length;
System.out.print(findMinDel(arr, n));
}
}
|
Python3
import sys
def findMinDel(arr, n) :
min_num = sys.maxsize;
for i in range (n) :
min_num = min (arr[i], min_num);
cnt = 0 ;
for i in range (n) :
if (arr[i] = = min_num) :
cnt + = 1 ;
return n - cnt;
if __name__ = = "__main__" :
arr = [ 3 , 3 , 2 ];
n = len (arr);
print (findMinDel(arr, n));
|
C#
using System;
class GFG
{
static int findMinDel( int []arr, int n)
{
int min_num = int .MaxValue;
for ( int i = 0; i < n; i++)
min_num = Math.Min(arr[i],
min_num);
int cnt = 0;
for ( int i = 0; i < n; i++)
if (arr[i] == min_num)
cnt++;
return n - cnt;
}
public static void Main(String[] args)
{
int []arr = { 3, 3, 2 };
int n = arr.Length;
Console.Write(findMinDel(arr, n));
}
}
|
Javascript
<script>
function findMinDel(arr, n)
{
var min_num = 1000000000;
for ( var i = 0; i < n; i++)
min_num = Math.min(arr[i], min_num);
var cnt = 0;
for ( var i = 0; i < n; i++)
if (arr[i] == min_num)
cnt++;
return n - cnt;
}
var arr = [3, 3, 2];
var n = arr.length;
document.write( findMinDel(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...