getting runtime error...

#include<stdio.h>

int main(){
  int t;
  long int n,m,i=0,large=0,small=0,j=0;
  //printf("enter the no of test cases");
  scanf("%d",&t);

  while(t--){
  //printf("enter the value of n and m");
  scanf("%ld %ld",&n,&m);
   int ar[n];
   int arr[m];
  for(i=0;i<m;i++){
   scanf("%d",&arr[i]);
  }

   for(i=0;i<=arr[0];i++){
   ar[i]=arr[0]-i;
   }
   if(arr[0]<n)
   {
       for(i=arr[0]+1;i<n;i++){
    ar[i]=i-arr[0];
   }
}
   if(m>1){
   if(arr[1]>arr[0])
   { large= arr[1];
     small= arr[0];
            for(j=large-((large-small)/2+1);j>=0;j--)
            {
                ar[j]=large-j;
            }
    if(m>2){
     for(i=2;i<m;i++){
       if(arr[i]<small){
            small=arr[i];
            for(j=small+((large-small)/2+1);j<n;j++){

                ar[j]=j-arr[i];
            }
       }
          else if(arr[i]>large){
             large=arr[i];
            for(j=large-((large-small)/2+1);j>=0;j--){

                ar[j]=j-arr[i];
            }
       }
      }
    }
   }
          else if(arr[1]<arr[0]){
            large=arr[0];
            small=arr[1];

            for(j= small+((large-small)/2+1);j<n;j++)
            {
                ar[j]=j-small;
            }
          if(m>2){
     for(i=2;i<m;i++){
       if(arr[i]<small){
            small=arr[i];
            for(j=small+((large-small)/2)+1;j<n;j++){

                ar[j]=j-arr[i];
            }
       }

       else if(arr[i]>large){
             large=arr[j];
            for(j=large-((large-small)/2)+1;j>=0;j--){

                ar[j]=j-arr[i];
            }
       }

     }
       }
       }
           }
       else if(arr[i]>=small && arr[i]<=large)
       continue;

        for(i=0;i<n;i++){
      printf("%d ",ar[i]);
      }
   printf("\n");
     }
       return 0;
 }

Please try to increase the readability of the post! Try to add some more specifications to your program like the language you used etc.

Please look at the problem constraints

1 ≤ T ≤ 10^4
1 ≤ N ≤ 10^5
1 ≤ M ≤ 10^5
1 ≤ Sum of N over all testcases ≤ 10^5
1 ≤ Sum of M over all testcases ≤ 10^5
0 ≤ Positions selected by captain ≤ N-1 

And in your code, you are initializing two arrays int ar[n]; int arr[m];
Consider the extreme cases that where the arrays need more memory than available, which might cause the runtime error you have got.Try to decrease the space complexity of your code which may resolve this error.Please recheck the algorithm you have used, the problem could be solved in O(n) time complexity and O(m) space complexityTo know better about complexities of algorithms, you could refer the linkshttp://en.wikipedia.org/wiki/Time_complexityhttp://en.wikipedia.org/wiki/Analysis_of_algorithms

I think the best ways to share a code is through ideone.com so we can easily run and debug your codes there :slight_smile:
So make up an account there and share the link of the code.

As per your code, it’s most probably array indexing going out of bounds according to me :slight_smile:

1 Like