Variable Sized Arrays

Sort by

recency

|

1435 Discussions

|

  • + 0 comments

    the question is not clear.

  • + 0 comments

    int n,q; cin>>n>>q; vector> arr(n); for (int i=0;i>size; arr[i].resize(size); for (int j=0; j>arr[i][j]; } } while (q--) { int x,y; cin>>x>>y; cout<

  • + 0 comments

    Hackerrank should hire people who can at least formulate the problem properly so other people could understand it.

  • + 0 comments

    int main() {

    int n,q;
    cin >> n >> q;
    vector<vector<int>>v(n);
    for(int i = 0; i < n;i++){
        int nums;
        cin >> nums;
        v[i].resize(nums);
        for(int j = 0; j < nums;j++){
            int x;
            cin >> x;
            v[i][j] = x;
        }     
    }
    while(q--){
        int a,b;
        cin >> a >> b;
        cout << v[a][b]<<endl;
    } 
    return 0;
    

    }

  • + 1 comment

    For anyone with doubts about the problem I will try to explain it (then I will attach a video to youtube if it's not enough).

    So it ask us for a 2 dimensional array, twice, in other words you will need to declare one 2d array and call it twice, "a" means how many arrays will be in the 2d array, and "q" is basically asking "Hey I will ask (q) number of times about the array, different question each time".

    The first line of input (2 2) means ; it will be a 2d array that contains “2” different arrays, and the other “2” means how many questions it's going to ask.

    The next line of inputs (3 1 5 4) and (5 1 2 8 9 3) is a bit more complicated. In the first line (3 1 5 4) Means two things, the first number (3 …) means how large will the array be, meaning that the next numbers are going to be the content of the array (... 1 5 4), in other words “3” is the size of the array, “1 5 4” is what is inside. It's the same with the second line, (5 1 2 8 9 3), “5” is the size of the array, and “... 1 2 8 9 3” is the content.

    For the last two lines of input (0 1) and (1 3) are coordinates, basically, in c++ a 2d array looks like “ int array [x] [y] “, the input (0 1) replaces “x” and “y” inside the array, so, the query is “ int array [0] [1] “, and in programming, the computer counts starting at 0, so “0” directs you to the 1st array of the 2d array (1 5 4), and “1” is the 2nd value of that array, so “ int array [0] [1] “ outputs “5”, the 2nd number of the 1st array. For the last two lines of input (0 1) and (1 3) are coordinates, basically, in c++ a 2d array looks like “ int array [x] [y] “, the input (0 1) replaces “x” and “y” inside the array, so, the query is “ int array [0] [1] “, and in programming, the computer counts starting at 0, so “0” directs you to the 1st array of the 2d array (1 5 4), and “1” is the 2nd value of that array, so “ int array [0] [1] “ outputs “5”, the 2nd number of the 1st array.

    Input (1 3) means “ array [1] [3] “, it outputs the 4th (3) number of the 2nd (1) array; if you are asking ‘What happened to the 3 and the 5 in the second and third line of input’ those just say how big the mentioned subsidiary array is.

    The youtube video: https://www.youtube.com/watch?v=IMsDWK0twOY