time limit exceeded in my code plz help me.......questn link is http://www.codechef.com/problems/AMSGAME1

#include<stdio.h>
int check(int *,int);
int main()
{
int t,n,x;
scanf("%d",&t);
while(t–)
{
scanf("%d",&n);
int p[1000]={0},j;
for(j=0;j<n;j++)
{
scanf("%d",&p[j]);
}
x=check(p,n);
while(x==0)
{
x=check(p,n);
}
printf("%d\n",x);
}
return 0;
}
int check(int *p,int n)
{
int i,min,max,t=0;
min=max=p[0];
for(i=1;i<n;i++)
{
if(p[i]<min)
min=p[i];
if(p[i]>max)
{
max=p[i];
t=i;
}
}
if(min==max)
return min;
else
{
p[t]=max-min;
return 0;
}
}

//