Saturday 18 July 2015

CANDY

Candy I

Link to the question : CANDY 

HINT :

Its obvious that if the number of chocolates is a multiple of the number of students then its possible to distribute equally among them. Now to count the number of moves so that each child get gets equal number of chocolates, we run a loop and all subtract all elements lesser than the average with the average. The summation of this will give us the number of moves.

RECOMMENDED QUESTION :

 Try solving this question related to LCM. 

SOURCE CODE :

#include<iostream>
using namespace std;
int main()
{
    int n=1,i;
    while(n!=-1)
    {
        cin>>n;
        if(n!=-1)
        {
            int a[n],s=0;
            for(i=0;i<n;i++)
            {
                cin>>a[i];
                s=s+a[i];
            }
            if(s%n!=0)
                cout<<"-1"<<endl;
            else
            {
                s=s/n;
                int m=0;
                for(i=0;i<n;i++)
                {
                    if(a[i]<s)
                        m=m+s-a[i];

                }
                cout<<m<<endl;
            }
        }
    }
    return 0;
}

No comments:

Post a Comment