getting segmented fault error in hackerrank problem- Variable Length Array

  c++, variable-length-array

https://www.hackerrank.com/challenges/variable-sized-arrays/problem

This is the problem statement.
I tried the following code

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int n,q;
    cin>>n>>q;
    int n1;
    int A[n][1000000];
   
for(int i =0; i<n; i++)   
{ cin>>n1;
     
    for(int j=0; j<n1; j++){
        int c;
        cin>>c;
        A[i][j] = c;
    }
}

int a,b;
for(int i=0;i<q; i++){
cin>>a>>b;
    
    cout<<A[a][b]<<"n";
}
    return 0;
}

This code passes the sample test case and other custom inputs(I have tried for small number of input values). But it does not work for Test cases in which the value of n and q (as mentioned in the problem) are large. It gives the "Segmented Fault" error. Can someone please explain why I am getting that error.

Source: Windows Questions C++

LEAVE A COMMENT