📜  从数组中删除最小数字以获取最小OR值

📅  最后修改于: 2021-04-26 04:52:12             🧑  作者: Mango

给定N个正整数的数组arr [] ,任务是找到要从数组中删除的元素的最小数量,以使数组元素的按位OR最小。不允许删除所有元素,即数组中至少必须保留一个元素。

例子:

天真的方法:生成所有可能的子序列,并测试哪一个给出最小的“ OR”值。令具有最小可能的最大子序列的长度为L,则答案将为N – L。这将花费指数时间。

更好的方法:最小值将始终等于数组中存在的最小值。如果此数字与除自身以外的任何其他数字按位进行“或”运算,则OR的值将更改,并且不再保持最小值。因此,我们需要删除不等于此最小元素的所有元素。

  • 在数组中找到最小的数字。
  • 在数组cnt中找到此元素的频率。
  • 最终答案将是N – cnt

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum
// deletions to get minimum OR
int 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 code
int main()
{
    int arr[] = { 3, 3, 2 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << findMinDel(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to return the minimum
// deletions to get minimum OR
static 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 code
public 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
# Python3 implementation of the approach 
import sys
  
# Function to return the minimum 
# deletions to get minimum OR 
def 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 code 
if __name__ == "__main__" : 
  
    arr = [ 3, 3, 2 ];
    n = len(arr);
      
    print(findMinDel(arr, n)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
// Function to return the minimum
// deletions to get minimum OR
static 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 code
public 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


输出:
2

时间复杂度: O(N)