<\/span><\/h2>\n\n\n\nConsider an n-element array, a, where each index i in the array contains a reference to an array of ki <\/sub>integers (where the value of varies from array to array). See the Explanation section below for a diagram.<\/p>\n\n\n\nGiven a, you must answer q queries. Each query is in the format i j, where i denotes an index in array a and j denotes an index in the array located at a[i]. For each query, find and print the value of element j in the array at location a[i] on a new line.<\/p>\n\n\n\n
<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains two space-separated integers denoting the respective values of n (the number of variable-length arrays) and q (the number of queries).<\/p>\n\n\n\n
Each line i of the n subsequent lines contains a space-separated sequence in the format k a[i]0<\/sub> a[i]1<\/sub> \u2026 a[i]k-1<\/sub> describing the k-element array located at a[i].<\/p>\n\n\n\nEach of the q subsequent lines contains two space-separated integers describing the respective values of i (an index in array a) and j (an index in the array referenced by a[i]) for a query.<\/p>\n\n\n\n
<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= n <= 105<\/sup><\/li>
- 1 <= q <= 105<\/sup><\/li>
- 1 <= k <= 3 . 105<\/sup><\/li>
- n <= \u03a3k <= 3 . 105<\/sup><\/li>
- 0 <= i < n<\/li>
- 0 <= j < k<\/li>
- All indices in this challenge are zero-based.<\/li>
- All the given numbers are non negative and are not greater than 106<\/sup>.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nFor each pair of i and j values (i.e., for each query), print a single integer that denotes the element located at index j of the array referenced by a[i]. There should be a total of q lines of output.<\/p>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\n2 2\n3 1 5 4\n5 1 2 8 9 3\n0 1\n1 3\n<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n5\n9\n<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\nThe diagram below depicts our assembled Sample Input:<\/p>\n\n\n\n
We perform the following q=2 queries:<\/p>\n\n\n\n
- Find the array located at index i = 0 , which corresponds to a[i] = [1,5,4]. We must print the value at index j=1 of this array which, as you can see, is 5.<\/li>
- Find the array located at index i=1 , which corresponds to a[i] = [1,2,8,9,3]. We must print the value at index j=3 of this array which, as you can see, is 9.<\/li><\/ol>\n\n\n\n
<\/span>Solution – Variable Sized Arrays Hackerrank Solution in C++<\/strong><\/span><\/h2>\n\n\n\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nint main() {\n \/\/ get length of array 'a' and number of queries\n int n, q;\n cin >> n >> q;\n\n \/\/ create vector of vectors\n vector<vector<int>> a(n);\n\n \/\/ fill each 2D vector i with k_i values\n for (int i = 0; i < n; i++) {\n \/\/ get the length k of the vector at a[i]\n int k;\n cin >> k;\n\n \/\/ fill the vector with k values\n a[i].resize(k);\n for (int j = 0; j < k; j++) {\n cin >> a[i][j];\n }\n }\n\n \/\/ run queries on a\n for (int q_num = 0; q_num < q; q_num++) {\n \/\/ get i, j as the 'query' to get a value from a\n int i, j;\n cin >> i >> j;\n cout << a[i][j] << endl;\n }\n\n return 0;\n}\n<\/pre>\n\n\n\n