i have been trying to work upon this problem.but everytime i post my solution i get wa.
please senior programmers help me.
my solution is;
#include<stdio.h>
#include
#include
using namespace std;
int main()
{ int t,a[3000],i,k,n,j,sum,r;
scanf("%d",&t);//first collect no of test cases
while(t–)//decrement test cases
{ sum=0,k=0;//initialise sum and repetetions to 0
scanf("%d",&n);//scan the number of idlis
for(i=0;i<n;i++)
scanf("%d",a+i);//scan the idlis one by one
sort(a,a+n);//sort idlis in increasing order
for(i=0;i<n;i++)
sum=sum+a[i];//find sum of idlis
if (sum%n!=0)
printf("-1\n");
else
{
for(i=0,j=n-1;j>=i;i++,j–)
{
if(a[i]==a[j])//when two values of max and min are equal then break
break;
r=(a[j]-a[i])/2 +(a[j]-a[i])%2;//find r…which is ciel(a[j]-a[i]/2)
a[j]=a[j]-r;//reduce a[j]by r
a[i]=a[i]+r;//increase a[i] by r
k++;//increase number of repetetions
}
sort(a,a+n);//sort the new array
if(a[0]==a[n-1])//if last element of sorted array id=s equal to first element i.e idlis are equal
printf("%d\n",k);
else
printf("-1\n");
}
}
return 0;
}