Hello coders, In this post, you will learn how to solve the **Variable Sized Arrays in C++ Hacker Rank Solution**. This problem is a part of the **HackerRank C++ Programming** Series.

## Variable Sized Arrays in C++ Hacker Rank Solution

### Problem

Consider an n-element array, * a*, where each index

*in the array contains a reference to an array of*

**i***integers (where the value of*

**ki***varies from array to array). See the Explanation section below for a diagram.*

**ki**Given

*, you must answer*

**a***queries. Each query is in the format i j, where*

**q***denotes an index in array*

**i***and*

**a***denotes an index in the array located at*

**j***For each query, find and print the value of element*

**a[i]***in the array at location*

**j***on a new line.*

**a[i]**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 n (the number of variable-length arrays) and q (the number of queries).Each line i of the n subsequent 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] for a query.

**Constraints**

**1<=n<=10^5****1<=q<=10^5****1<=k<=3.10^5****n<=k<=3.10^5****0<=i<n****0<=j<k**- All indices in this challenge are zero-based.
- All the given numbers are non negative and are not greater than
**10^6**

**Output Format**

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

#### Sample Input :

2231545128930113

#### Sample Output :

59

#### Explanation :

The diagram below depicts our assembled Sample Input:

We perform the following * q = 2* queries:

- Find the array located at index
, which corresponds to**i =0**. We must print the value at index**a[0] = [1,5,4]**of this array which, as you can see, is 5.**j=1** - Find the array located at index
, which corresponds to**i=1**. We must print the value at index**a[1]=[1,2,8,9,3]**of this array which, as you can see, is**j = 3**.**9**

**Variable Sized Arrays in C++ Hacker Rank Solution**

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n; int q; cin >> n >> q; vector<int> a[n]; for(int i = 0; i < n; i++){ int m; cin >> m; int o; for(int j = 0; j < m; j++){ cin >> o; a[i].push_back(o); } } int r, s; for(int k = 1; k <= q; k++){ cin >> r >> s; cout << a[r][s] << endl; } return 0; }

**Disclaimer: **The above Problem **(Variable Sized Arrays in C++**) is generated by **Hackerrank** but the Solution is Provided by **Chase2Learn**. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.