Anayat And His Girl Friend[CDCR15_4]

link

why am i getting TLE.Please help.
my code:-

#define gtc getchar()

int main()
{
int t,i;
int n,d,min,max,diff,maxi,mini,cur;

for(scanf("%d\n",&t);t>0;t--) {
	
	scanf("%d %d\n",&n,&d);
	scanf("%d",&min);
	max = min;
	maxi = 0;mini = 0;
	for(i = 1; i < n ; i++) {
		if((max - min) >= d) break;
		//cur = read_int();
		scanf("%d",&cur);
			if(max < cur) {max = cur; maxi = i;}  
			else {
				if(min > cur) {min = cur ;mini = i; }
			}
		 
	}
	if((i<n)) while( (gtc != '\n'));

	

	if((max - min) < d) printf("%d\n",(n/2 +n%2));
	
	else if(maxi > mini) {
		diff = maxi - mini;
		printf("%d\n",(mini/2 + diff/2 + mini%2 + diff%2+1));
	}

	else {
		diff = mini - maxi;
		printf("%d\n",(maxi/2 + diff/2 + maxi%2 + diff%2+1));
	}
}
return 0;

}