Hackerrank Variable Sized Arrays Solution

Hackerrank Variable Sized Arrays Solution

.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

Consider an -element array, , where each index  in the array contains a reference to an array of  integers (where the value of  varies from array to array). See the Explanation section below for a diagram.

Given , you must answer  queries. Each query is in the format i j, where  denotes an index in array  and  denotes an index in the array located at . For each query, find and print the value of element  in the array at location  on a new line.

Click here to know more about how to create variable sized arrays in C++.

Input Format

The first line contains two space-separated integers denoting the respective values of  (the number of variable-length arrays) and  (the number of queries).
Each line  of the  subsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the -element array located at .
Each of the  subsequent lines contains two space-separated integers describing the respective values of  (an index in array ) and  (an index in the array referenced by ) for a query.

Constraints.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

  • All indices in this challenge are zero-based.
  • All the given numbers are non negative and are not greater than

Output Format

For each pair of  and  values (i.e., for each query), print a single integer denoting the element located at index  of the array referenced by . There should be a total of  lines of output.

Sample Input

2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3

Sample Output

5
9

Explanation

The diagram below depicts our assembled Sample Input:

variable-length-arrays.png

We perform the following  queries:

  1. Find the array located at index , which corresponds to . We must print the value at index  of this array which, as you can see, is .
  2. Find the array located at index , which corresponds to . We must print the value at index  of this array which, as you can see, is .

Solution in cpp

Approach 1.

int n,q;
cin >> n>> q;
int** seq = new int* [n];
for(int i=0;i<n;i++) {
    int a;
    cin>>a;
    int* b=new int [a];
    for(int j=0;j<a;j++) {
        int e;
        cin>>e;
        b[j]=e;
    }
    *(seq+i)=b;
}
for(int i=0;i<q;i++) {
  int r,s;
  cin >> r >> s;
  cout << seq[r][s] << endl;
}
return 0;
}

Approach 2.

    int n, q, k, qs, qi;
    cin >> n >> q;
    int **seq = new int*[n];
    for (int c = 0; c < n; c++) {
        cin >> k;
        seq[c] = new int[k];
        for (int d = 0; d < k; d++) {
            cin >> seq[c][d];
        }
    }
    for (int c = 0; c < q; c++) {
        cin >> qs >> qi;
        cout << seq[qs][qi] << endl;
    }
	return 0;
}

Approach 3.

    int n, q;
    cin >> n >> q;

    int *mtr[n];

    for(int i=0; i<n; i++){
        int s;
        cin >> s;
        mtr[i] = new int[s];
        for(int j=0; j<s; j++){
            cin>>mtr[i][j];
        }          
    }

    for(int i=0; i<q; i++){
        int r, c;
        cin >> r;
        cin >> c;
        cout << mtr[r][c]<<endl;
    }
    
	return 0;
}

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe