📌  相关文章
📜  删除一个元素以获得最小OR值

📅  最后修改于: 2021-05-04 11:48:25             🧑  作者: Mango

给定一个由N个元素组成的数组arr [] ,任务是从数组中删除一个元素,以使该数组的OR值最小。打印最小值。

例子:

天真的方法:一种方法是一个一个地删除每个元素,然后找到其余元素的OR。该方法的时间复杂度将为O(N 2 )。

高效方法:要有效解决问题,必须为任何元素arr [i]确定(OR(arr [0…i-1])| OR(arr [i + 1…N-1]))的值。为此,他添加了前缀,可以通过pre []suf []计算后缀OR数组,其中pre [i]存储OR(arr [0…i]),suff [i]存储OR(arr [i…N -1]) 。然后,删除第i元素后的数组的OR值可以计算为(pre [i-1] | suff [i + 1]) ,并且答案将是所有可能的OR值中的最小值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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


输出:
3