Given an array arr[] of N integers and an integer K, the task is to find whether the given array elements can be made 0 if the given operation is applied exactly K times. In a single operation, the smallest element from the array will be subtracted from all the non-zero elements of the array.
Examples:
Input: arr[] = {1, 1, 2, 3}, K = 3
Output: Yes
K = 1 -> arr[] = {0, 0, 1, 2}
K = 2 -> arr[] = {0, 0, 0, 1}
K = 3 -> arr[] = {0, 0, 0, 0}
Input: arr[] = {11, 2, 3, 4}, K = 3
Output: No
The array requires 4 operations.
Approach:
- Major observation here is that the minimum number does not matter at each operation, suppose X is the minimum number then all the occurrences of X will be 0 in the current operation and other elements will reduce by X.
- We can conclude that all the identical elements will be 0 at the same operation.
- So, say in Q operations the entire array becomes 0, it is equal to the number of unique elements in the array.
- If Q = K then the answer is Yes else print No.
- Number of unique elements can be obtained using set.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int arr[], int N, int K)
{
set< int > unique;
for ( int i = 0; i < N; i++)
unique.insert(arr[i]);
if (unique.size() == K)
return true ;
return false ;
}
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
import java.util.*;
class GFG
{
static boolean check( int arr[], int N, int K)
{
HashSet<Integer> unique = new HashSet<Integer>();
for ( int i = 0 ; i < N; i++)
unique.add(arr[i]);
if (unique.size() == K)
return true ;
return false ;
}
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" );
}
}
|
Python3
def check(arr, N, K):
unique = dict ()
for i in range (N):
unique[arr[i]] = 1
if len (unique) = = K:
return True
return False
arr = [ 1 , 1 , 2 , 3 ]
N = len (arr)
K = 3
if (check(arr, N, K) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static bool check( int [] arr, int N, int K)
{
HashSet< int > unique = new HashSet< int >();
for ( int i = 0; i < N; i++)
{
unique.Add(arr[i]);
}
if (unique.Count == K)
{
return true ;
}
return false ;
}
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" );
}
}
}
|
PHP
<?php
function check( & $arr , $N , $K )
{
$unique = array_unique ( $arr );
if ( count ( $unique ) == $K )
return true;
return false;
}
$arr = array ( 1, 1, 2, 3 );
$N = count ( $arr );
$K = 3;
if (check( $arr , $N , $K ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function check(arr, N, K)
{
var unique = new Set();
for ( var i = 0; i < N; i++)
unique.add(arr[i]);
if (unique.size == K)
return true ;
return false ;
}
var arr = [1, 1, 2, 3];
var N = arr.length;
var K = 3;
if (check(arr, N, K))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(nlogn), where n is the size of the given array.
Auxiliary Space: O(n), where extra space of size n is used to create a set.