Skip to content
Related Articles

Related Articles

Remove one element to get minimum OR value
  • Last Updated : 27 Dec, 2019

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, he prefix and the suffix OR arrays can be calcualted 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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimized OR
// after removing an element from the array
int minOR(int* arr, int n)
{
    // Base case
    if (n == 1)
        return 0;
  
    // Prefix and suffix OR array
    int pre[n], suf[n];
    pre[0] = arr[0], suf[n - 1] = arr[n - 1];
  
    // Computing prefix/suffix OR arrays
    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]);
  
    // To store the final answer
    int ans = min(pre[n - 2], suf[1]);
  
    // Finding the final answer
    for (int i = 1; i < n - 1; i++)
        ans = min(ans, (pre[i - 1] | suf[i + 1]));
  
    // Returning the final answer
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << minOR(arr, n);
  
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
  
// Function to return the minimized OR
// after removing an element from the array
static int minOR(int []arr, int n)
{
    // Base case
    if (n == 1)
        return 0;
  
    // Prefix and suffix OR array
    int []pre = new int[n];
    int []suf = new int[n];
    pre[0] = arr[0];
    suf[n - 1] = arr[n - 1];
  
    // Computing prefix/suffix OR arrays
    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]);
  
    // To store the final answer
    int ans = Math.min(pre[n - 2], suf[1]);
  
    // Finding the final answer
    for (int i = 1; i < n - 1; i++)
        ans = Math.min(ans, (pre[i - 1] | 
                             suf[i + 1]));
  
    // Returning the final answer
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3 };
    int n = arr.length;
  
    System.out.print(minOR(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3




# Python3 implementation of the approach
  
# Function to return the minimized OR
# after removing an element from the array
def minOR(arr, n):
      
    # Base case
    if (n == 1):
        return 0
  
    # Prefix and suffix OR array
    pre = [0] * n
    suf = [0] * n
    pre[0] = arr[0]
    suf[n - 1] = arr[n - 1]
  
    # Computing prefix/suffix OR arrays
    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])
  
    # To store the final answer
    ans = min(pre[n - 2], suf[1])
  
    # Finding the final answer
    for i in range(1, n - 1):
        ans = min(ans, (pre[i - 1] | suf[i + 1]))
  
    # Returning the final answer
    return ans
  
# Driver code
if __name__ == '__main__':
    arr = [1, 2, 3]
    n = len(arr)
  
    print(minOR(arr, n))
  
# This code is contributed by Mohit Kumar


C#




// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to return the minimized OR 
    // after removing an element from the array 
    static int minOR(int []arr, int n) 
    
        // Base case 
        if (n == 1) 
            return 0; 
      
        // Prefix and suffix OR array 
        int []pre = new int[n];
        int []suf = new int[n]; 
          
        pre[0] = arr[0];
        suf[n - 1] = arr[n - 1]; 
      
        // Computing prefix/suffix OR arrays 
        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]); 
      
        // To store the final answer 
        int ans = Math.Min(pre[n - 2], suf[1]); 
      
        // Finding the final answer 
        for (int i = 1; i < n - 1; i++) 
            ans = Math.Min(ans, (pre[i - 1] | 
                                 suf[i + 1])); 
      
        // Returning the final answer 
        return ans; 
    
      
    // Driver code 
    static public void Main ()
    
        int []arr = { 1, 2, 3 }; 
        int n = arr.Length; 
      
        Console.WriteLine(minOR(arr, n)); 
    
}
  
// This code is contributed by AnkitRai01


Output:

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up