Here is my code which gives TLE in c but is accepted in c++. How can i make it run within time limit in c as it is my preferred language.
#include<stdio.h>
int main()
{
int test,n,count;
scanf("%d",&test);
while(test--)
{
count=0;
int max=0,min=9999,maxi,mini;
scanf("%d",&n);
int arr[n],sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
sum+=arr[i];
}
if(sum%n!=0)
{
printf("-1\n");
continue;
}
//printf("Min = %d Max = %d \n",min,max);
do
{
max=0,min=9999;
for(int i=0;i<n;i++)
{
if(arr[i]>=max)
{
max=arr[i];
maxi=i;
}
if(arr[i]<=min)
{
min=arr[i];
mini=i;
}
}
int temp=(max-min+1)/2;
//temp--;
arr[mini]+=temp;
arr[maxi]-=temp;
count++;
}while(max!=min);
printf("%d\n",count-1);
}
}