Runtime error in little elephant and median problem while it runs successfully on my computer?

Why my code written in c is giving runtime error while it is running successfully on my computer…
the source code is

#include<stdio.h>
int main()
{
int x,g,a[30],b[30],c[30],t,i,j,key,n;

scanf("%d",&t);
x=0;
g=t;
while(t>0)
{

scanf("%d",&n);

for(i=0;i<n;i++) 
  {
  scanf("%d",&a[i]);               
  }
  for(i=0;i<n;i++)
  {
  b[i]=a[i];                
  }   
    for(j=1;j<n;j++)
    {
    key=b[j];
    i=j-1;
    while(i>-1&&b[i]>key)
       {
       b[i+1]=b[i];
       i--;                  
       }   
       b[i+1]=key;
                    
                    
    }
   int l,k,p;
   c[x]=0; 
   int max=b[n-1];
   k=n-2;
   p=1;
   while(b[k]==max)
   {
     k--;
     p++;                
                   
   }
   
   while(1)
   {
   l=p;
   while(k>=0&&l>0)        
      {
      b[k]=max;
      k--;
      p++;
      l--;                     
      }     
      c[x]++;
      if(k==-1)
      break;     
   }

      
 t--;
 x++;
}
for(i=0;i<g;i++)
{
printf("%d\n",c[i]);
 }
   
    
}

problem link

Why my code written in c is giving runtime error while it is running successfully on my computer

probably because it was not fed with the exactly same data. :slight_smile:

i’ll try to have a look at your code if i find time. did you find out since ?

Try the following input and follow the logic of your program:

1

1

1

The correct result is:

0

, because the array already satisfies the condition. I have not checked whether there are other problems with the logic, but this test case should produce some kind of run time error with your code.

Good luck.

1 Like