Algorithm


Problem link- https://www.spoj.com/problems/CANDY3/

CANDY3 - Candy III

 

A class went to a school trip. And, as usually, all N kids have got their backpacks stuffed with candy. But soon quarrels started all over the place, as some of the kids had more candies than others. Soon, the teacher realized that he has to step in: "Everybody, listen! Put all the candies you have on this table here!"

Soon, there was quite a large heap of candies on the teacher's table. "Now, I will divide the candies into N equal heaps and everyone will get one of them." announced the teacher.

"Wait, is this really possible?" wondered some of the smarter kids.

Problem specification

You are given the number of candies each child brought. Find out whether the teacher can divide the candies into N exactly equal heaps. (For the purpose of this task, all candies are of the same type.)

Input specification

The first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line.

Each test case looks as follows: The first line contains N : the number of children. Each of the next N lines contains the number of candies one child brought.

Output specification

For each of the test cases output a single line with a single word "YES" if the candies can be distributed equally, or "NO" otherwise.

Example

Input:
2

5
5
2
7
3
8

6
7
11
2
7
3
4

Output:
YES
NO
Note: the input file will not exceed 1MB

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming

import java.util.*;
class candy3
{
	public static void main(String args[])
	{
		int t;
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		System.out.println();
		String[] s=new String[t];
		int n;
		try
		{
			for(int i=0;i<t;i++)
			{
				n=sc.nextInt();
				long[] abc=new long[n];
				long sum=0;
				for(int j=0;j<n;j++)
				{
					abc[j]=sc.nextLong();
					sum=(sum+abc[j])%n;
				}
				if(sum==0)
				{
					s[i]="YES";
				}
				else
				{
					s[i]="NO";
				}
				System.out.println();
			}
			for(int i=0;i<t;i++)
			{
				System.out.println(s[i]);
			}
		}
		catch(Exception e)
		{
			return;
		}
	}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2
5
5
2
7
3
8
6
7
11
2
7
3
4

Output

x
+
cmd
YES
NO

#2 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include<cstdio>
using namespace std;

int main() {
int t;
long long int m,n, p,s;
scanf("%d",&t);
while(t--)
{
   
s=0;
scanf("%lld",&n);
m=n;
while(n--)
{
scanf("%lld",&p);
s=s+p;
s%=m;
}
if(s%m==0)
printf("YES\n");
else
printf("NO\n");
}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2
5
5
2
7
3
8
6
7
11
2
7
3
4

Output

x
+
cmd
YES
NO
Advertisements

Demonstration


SPOJ Solution-Candy III-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python