## Algorithm

Problem Name: Variable Sized Arrays

In this HackerRank Functions in C++ programming problem solution,

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

Given 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.

Input Format

The 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).
Each line i of the n ubsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the k-element array located at a[i].

Each 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] or a query.

Constraints

• 1 <= n <=10**5
• 1 <= q <=10**5
• 1 <= k <=3*10**5

Sample Input

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


Sample Output

5
9


## Code Examples

### #1 Code Example with C++ Programming

Code - C++ Programming


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

int main(void)
{
int n, q;
cin >> n >> q;

vector <  vector<int> > arr(n);

for(int i = 0; i  <  n; i++){
int k, val;
cin >> k;
for(int j = 0; j  <  k; j++){
cin >> val;
arr[i].push_back(val);
}
}

int m, p;
for(int i = 0; i  <  q; i++){
cin >> m >> p;
cout << arr[m][p] << endl;
}

return 0;
}

Copy The Code &

Input

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

Output

cmd
5 9