# Variable Sized Arrays Hackerrank Solution in C++ | Hackerrank

Variable Sized Arrays Hackerrank Solution in C++. 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. Hackerrank Variable Sized Arrays Solution.

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

Sample Input

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

Sample Output

5
9

## Variable Sized Arrays Hackerrank Solution in C++

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

int main()
{
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;
}
}``````

### Similar to Variable Sized Arrays

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.