Remove one element to get minimum OR value
Given an array arr[] of N elements, the task is to remove one element from the array such that the OR value of the array is minimized. Print the minimized value.
Examples:
Input: arr[] = {1, 2, 3}
Output: 3
All possible ways of deleting one element and their
corresponding OR values will be:
a) Remove 1 -> (2 | 3) = 3
b) Remove 2 -> (1 | 3) = 3
c) Remove 3 -> (1 | 2) = 3
Thus, the answer will be 3.
Input: arr[] = {2, 2, 2}
Output: 2
Naive approach: One way will be to remove each element one by one and then finding the OR of the remaining elements. The time complexity of this approach will be O(N2).
Efficient approach: To solve the problem efficiently, the value of (OR(arr[0…i-1]) | OR(arr[i+1…N-1])) has to be determined for any element arr[i]. To do so, the prefix and the suffix OR arrays can be calculated say pre[] and suf[] where pre[i] stores OR(arr[0…i]) and suff[i] stores OR(arr[i…N-1]). Then the OR value of the array after deleting the ith element can be calculated as (pre[i-1] | suff[i+1]) and the answer will be minimum of all the possible OR values.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOR( int * arr, int n)
{
if (n == 1)
return 0;
int pre[n], suf[n];
pre[0] = arr[0], suf[n - 1] = arr[n - 1];
for ( int i = 1; i < n; i++)
pre[i] = (pre[i - 1] | arr[i]);
for ( int i = n - 2; i >= 0; i--)
suf[i] = (suf[i + 1] | arr[i]);
int ans = min(pre[n - 2], suf[1]);
for ( int i = 1; i < n - 1; i++)
ans = min(ans, (pre[i - 1] | suf[i + 1]));
return ans;
}
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof (arr) / sizeof ( int );
cout << minOR(arr, n);
return 0;
}
|
Java
class GFG
{
static int minOR( int []arr, int n)
{
if (n == 1 )
return 0 ;
int []pre = new int [n];
int []suf = new int [n];
pre[ 0 ] = arr[ 0 ];
suf[n - 1 ] = arr[n - 1 ];
for ( int i = 1 ; i < n; i++)
pre[i] = (pre[i - 1 ] | arr[i]);
for ( int i = n - 2 ; i >= 0 ; i--)
suf[i] = (suf[i + 1 ] | arr[i]);
int ans = Math.min(pre[n - 2 ], suf[ 1 ]);
for ( int i = 1 ; i < n - 1 ; i++)
ans = Math.min(ans, (pre[i - 1 ] |
suf[i + 1 ]));
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
System.out.print(minOR(arr, n));
}
}
|
Python3
def minOR(arr, n):
if (n = = 1 ):
return 0
pre = [ 0 ] * n
suf = [ 0 ] * n
pre[ 0 ] = arr[ 0 ]
suf[n - 1 ] = arr[n - 1 ]
for i in range ( 1 , n):
pre[i] = (pre[i - 1 ] | arr[i])
for i in range (n - 2 , - 1 , - 1 ):
suf[i] = (suf[i + 1 ] | arr[i])
ans = min (pre[n - 2 ], suf[ 1 ])
for i in range ( 1 , n - 1 ):
ans = min (ans, (pre[i - 1 ] | suf[i + 1 ]))
return ans
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ]
n = len (arr)
print (minOR(arr, n))
|
C#
using System;
class GFG
{
static int minOR( int []arr, int n)
{
if (n == 1)
return 0;
int []pre = new int [n];
int []suf = new int [n];
pre[0] = arr[0];
suf[n - 1] = arr[n - 1];
for ( int i = 1; i < n; i++)
pre[i] = (pre[i - 1] | arr[i]);
for ( int i = n - 2; i >= 0; i--)
suf[i] = (suf[i + 1] | arr[i]);
int ans = Math.Min(pre[n - 2], suf[1]);
for ( int i = 1; i < n - 1; i++)
ans = Math.Min(ans, (pre[i - 1] |
suf[i + 1]));
return ans;
}
static public void Main ()
{
int []arr = { 1, 2, 3 };
int n = arr.Length;
Console.WriteLine(minOR(arr, n));
}
}
|
Javascript
<script>
function minOR(arr, n)
{
if (n == 1)
return 0;
var pre = Array(n), suf = Array(n);
pre[0] = arr[0], suf[n - 1] = arr[n - 1];
for ( var i = 1; i < n; i++)
pre[i] = (pre[i - 1] | arr[i]);
for ( var i = n - 2; i >= 0; i--)
suf[i] = (suf[i + 1] | arr[i]);
var ans = Math.min(pre[n - 2], suf[1]);
for ( var i = 1; i < n - 1; i++)
ans = Math.min(ans, (pre[i - 1] | suf[i + 1]));
return ans;
}
var arr = [1, 2, 3];
var n = arr.length;
document.write( minOR(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Space Optimized Approach:
Count frequency of set bit of each element in 32 size array and traverse the array from most significant bit and check if any bit has frequency one, if we found such element than remove that particular element. The left element will participate in our actual result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOR( int arr[], int n)
{
int helper[32];
memset (helper, 0, sizeof (helper));
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < 32; j++) {
if ((arr[i] & (1 << j))) {
helper[j]++;
}
}
}
int first_index = -1;
for ( int i = 31; i >= 0; i--) {
if (helper[i] == 1) {
first_index = i;
break ;
}
}
int sum = 0;
if (first_index == -1) {
for ( int i = 31; i >= 0; i--) {
sum = sum | arr[i];
}
}
else {
for ( int i = 0; i < n; i++) {
if ((arr[i] & (1 << first_index))) {
continue ;
}
else {
sum = sum | arr[i];
}
}
}
return sum;
}
int main()
{
int a[] = { 4, 9, 8 };
int n = sizeof (a) / sizeof (a[0]);
int ans = minOR(a, n);
cout << ans << endl;
}
|
Java
import java.util.*;
public class GFG {
static int minOR( int [] arr, int n)
{
int [] helper = new int [ 32 ];
for ( int i = 0 ; i < 32 ; i++) {
helper[i] = 0 ;
}
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < 32 ; j++) {
if ((arr[i] & ( 1 << j)) != 0 ) {
helper[j]++;
}
}
}
int first_index = - 1 ;
for ( int i = 31 ; i >= 0 ; i--) {
if (helper[i] == 1 ) {
first_index = i;
break ;
}
}
int sum = 0 ;
if (first_index == - 1 ) {
for ( int i = 31 ; i >= 0 ; i--) {
sum = sum | arr[i];
}
}
else {
for ( int i = 0 ; i < n; i++) {
if ((arr[i] & ( 1 << first_index)) != 0 ) {
continue ;
}
else {
sum = sum | arr[i];
}
}
}
return sum;
}
public static void main(String args[])
{
int [] a = { 4 , 9 , 8 };
int n = a.length;
int ans = minOR(a, n);
System.out.println(ans);
}
}
|
C#
using System;
class GFG {
static int minOR( int [] arr, int n)
{
int [] helper = new int [32];
for ( int i = 0; i < 32; i++) {
helper[i] = 0;
}
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) != 0) {
helper[j]++;
}
}
}
int first_index = -1;
for ( int i = 31; i >= 0; i--) {
if (helper[i] == 1) {
first_index = i;
break ;
}
}
int sum = 0;
if (first_index == -1) {
for ( int i = 31; i >= 0; i--) {
sum = sum | arr[i];
}
}
else {
for ( int i = 0; i < n; i++) {
if ((arr[i] & (1 << first_index)) != 0) {
continue ;
}
else {
sum = sum | arr[i];
}
}
}
return sum;
}
public static void Main()
{
int [] a = { 4, 9, 8 };
int n = a.Length;
int ans = minOR(a, n);
Console.WriteLine(ans);
}
}
|
Javascript
function minOR(arr, n)
{
let helper= new Array(32).fill(0);
for (let i = 0; i < n; i++) {
for (let j = 0; j < 32; j++) {
if ((arr[i] & (1 << j))) {
helper[j]++;
}
}
}
let first_index = -1;
for (let i = 31; i >= 0; i--) {
if (helper[i] == 1) {
first_index = i;
break ;
}
}
let sum = 0;
if (first_index == -1) {
for (let i = 31; i >= 0; i--) {
sum = sum | arr[i];
}
}
else {
for (let i = 0; i < n; i++) {
if ((arr[i] & (1 << first_index))) {
continue ;
}
else {
sum = sum | arr[i];
}
}
}
return sum;
}
let a = [ 4, 9, 8 ];
let n = a.length;
let ans = minOR(a, n);
console.log(ans);
|
Python3
def minOR(arr, n):
helper = [ 0 ] * 32 ;
for i in range ( 0 ,n):
for j in range ( 0 , 32 ):
if ((arr[i] & ( 1 << j))):
helper[j] + = 1 ;
first_index = - 1 ;
i = 31 ;
while (i> = 0 ):
if (helper[i] = = 1 ):
first_index = i;
break ;
i = i - 1 ;
sum = 0 ;
if (first_index = = - 1 ):
i = 31 ;
while (i> = 0 ):
sum = sum | arr[i];
i - = 1
else :
for i in range ( 0 ,n):
if ((arr[i] & ( 1 << first_index))) :
continue ;
else :
sum = sum | arr[i];
return sum ;
a = [ 4 , 9 , 8 ];
n = len (a);
ans = minOR(a, n);
print (ans);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
30 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...