Problem Description
"Given n numbers, you can perform the following operation any number of times : Choose any subset of the numbers (possibly empty), none of which are 0. Decrement the numbers in the subset by 1, and increment the numbers not in the subset by K.
Is it possible to perform operations such that exactly n - 1 numbers become 0 ?CODING ARENA
#include <stdio.h>
int main()
{
int t,n,k,a[100],hash[1000],i,ans,x;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&k);
for(i=0;i<1000;i++)hash[i]=0;
ans=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
x=(a[i]%(k+1));
hash[x]++;
if(hash[x]>n-2)ans=1;
}
if(ans)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Test Case 1
Input (stdin)3 2 1 10 10 3 2 1 2 2 3 2 1 2 3
Expected OutputYES YES NO
Test Case 2
Input (stdin)3 2 1 4 3 2 4 1 2 2 3 2 3 1 2 4 4
Expected OutputYES YES YES
No comments:
Post a Comment