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.
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 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
Submit your solution here: Click here
Variable Sized Arrays Hackerrank Solution in C++
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
//Ghanendra Yadav
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;
}
}
The Output of Variable Sized Arrays Hackerrank Solution
Similar to Variable Sized Arrays
- Conditional Statements in C++ Hackerrank Solution
- Reverse a Linked List Hackerrank Solution
- Input And Output Hackerrank Solution in C++
- Solve Me First Hackerrank Solution in C & C++
- A Very Big Sum Hackerrank Solution C++
- Deque STL Hackerrank Solution in C++
- Mini Max Sum Hackerrank Solution in C++
- Staircase Hackerrank Solution in C
- Attribute Parser Hackerrank Solution in C++
- Inheritance Introduction Hackerrank Solution in C++
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
0 Comments: