Why am I getting runtime error in this problem and how can I not get that?

#include <bits/stdc++.h>
using namespace std;
#define ll int
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll T;
cin >> T;
while(T–){
ll n;
ll arra[n];
ll arrb[n];
cin >> n;
for(int i = 0 ; i < n ; i++){cin >> arra[i];}
for(int i = 0 ; i < n ; i++){cin >> arrb[i];}
sort(arra,arra+n);
ll o = 0 ;
for(ll j = 0 ; j < n ; j++){
if(arra[j]!=arrb[j]){
o++;
cout << “no”<< endl;
break;
}

   }
   if(o==0){cout << "yes" << endl;}
}

return 0;
}

Problem Code: SRTCHK

Can you share the problem link?

Check the following solution.

Accepted

1 Like

Note that ISO C++ forbids using variable-length arrays.

1 Like

Thanks a lot. This code worked simply by changing the array into vector. I still don’t understand why it worked. In this problem we don’t require variable length arrays.

With pleasure. An array length that is unknown at compile-time is called variable-length array. The compiler needs this information to allocate the required memory space for the array in the program stack memory space. The vector standard-template library class does not use the stack memory space to allocate objects at run-time. Instead, the needed memory is allocated from the heap memory space using dynamic memory allocation.
Best wishes.

1 Like

I’m a developer at Paragon Education Institute, and this knowledge is really useful to me. Thank you very much.

The Paragon Education Agency requires the services of a graphic designer. Please get in touch with me if you are interested and skilled.