-1
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
int t;
cin>>t;

while(t--)
{
    ll n, a[10000000];
    cin>>n;

    for(int i=0;i<n;i++)
    cin>>a[i];

    for(int i=0;i<n-1;i++)
    {
        int flag=0;
        for(int j=i+1;j<n;j++)
        {
            if(a[j] > a[i])
            {
                cout<<a[j]<<" ";
                flag=1;
                break;
            }
        }

        if(flag==0)
         cout<<"-1 ";
}
cout<<"-1\n";
}
return 0;
}

why i am getting segmentation fault using this code? is there anything wrong in the code. as per my knowledge there is nothing wrong in code. please do explain the fault...

Gowtham Gowda
  • 1,780
  • 1
  • 10
  • 7

1 Answers1

1

a[10000000] is probably too large to have automatic storage duration. Regard something of the order of 1Mb as the limit.

The fix is to replace the first two lines of your loop with

std::size_t n; // using a macro is a bad idea
std::cin >> n; // no professional programmer uses `using namespace std`
std::vector<long long/*using a macro is a bad idea*/> a(n);

noting that you ought to deal with any exceptions to the std::vector construction if n is too large.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483