📌  相关文章
📜  检查给定数组是否可以通过给定次数执行给定操作而减少为零

📅  最后修改于: 2021-04-29 11:31:39             🧑  作者: Mango

给定一个由N个整数和整数K组成的数组arr [] ,任务是查找如果给定的操作恰好应用了K次,是否可以使给定的数组元素为0。在一次操作中,将从数组中所有非零元素中减去数组中最小的元素。

例子:

方法:

  • 此处的主要观察结果是每次操作的最小数目无关紧要,假设X是最小数目,那么在当前操作中X的所有出现都将为0 ,而其他元素将减少X。
  • 我们可以得出结论,在相同的操作中所有相同的元素将为0。
  • 因此,假设在Q操作中整个数组变为0 ,它等于数组中唯一元素的数量。
  • 如果Q = K,则答案为”,否则打印“否”
  • 可以使用set获得唯一元素的数量。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if the array
// can be reduced to 0s with the given
// operation performed given number of times
bool check(int arr[], int N, int K)
{
    // Set to store unique elements
    set unique;
  
    // Add every element of the array
    // to the set
    for (int i = 0; i < N; i++)
        unique.insert(arr[i]);
  
    // Count of all the unique elements
    // in the array
    if (unique.size() == K)
        return true;
    return false;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 1, 2, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
    if (check(arr, N, K))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*;
  
class GFG
{
      
// Function that returns true if the array 
// can be reduced to 0s with the given 
// operation performed given number of times 
static boolean check(int arr[], int N, int K) 
{ 
    // Set to store unique elements 
    HashSet unique = new HashSet();
  
  
    // Add every element of the array 
    // to the set 
    for (int i = 0; i < N; i++) 
        unique.add(arr[i]); 
  
    // Count of all the unique elements 
    // in the array 
    if (unique.size() == K) 
        return true; 
    return false; 
} 
  
// Driver code 
public static void main(String[] args) 
{
    int arr[] = { 1, 1, 2, 3 }; 
    int N = arr.length; 
    int K = 3; 
    if (check(arr, N, K)) 
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function that returns true if the array
# can be reduced to 0s with the given
# operation performed given number of times
def check(arr, N, K):
      
    # Set to store unique elements
    unique = dict()
  
    # Add every element of the array
    # to the set
    for i in range(N):
        unique[arr[i]] = 1
  
    # Count of all the unique elements
    # in the array
    if len(unique) == K:
        return True
    return False
  
# Driver code
arr = [1, 1, 2, 3]
N = len(arr)
K = 3
if (check(arr, N, K) == True):
    print("Yes")
else:
    print("No")
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach 
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function that returns true if the array 
// can be reduced to 0s with the given 
// operation performed given number of times 
public static bool check(int[] arr, int N, int K)
{
    // Set to store unique elements 
    HashSet unique = new HashSet();
  
  
    // Add every element of the array 
    // to the set 
    for (int i = 0; i < N; i++)
    {
        unique.Add(arr[i]);
    }
  
    // Count of all the unique elements 
    // in the array 
    if (unique.Count == K)
    {
        return true;
    }
    return false;
}
  
// Driver code 
public static void Main(string[] args)
{
    int[] arr = new int[] {1, 1, 2, 3};
    int N = arr.Length;
    int K = 3;
    if (check(arr, N, K))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
  
// This code is contributed by shrikanth13


PHP


输出:
Yes