Variable Sized Arrays in C++ | HackerRank Solution

Hello there, today we are going to solve Variable Sized Arrays Hacker Rank Solution in C++.

Hello there, today we are going to solve Variable Sized Arrays Hacker Rank Solution in C++.

Variable Sized Arrays in C++

Table Of Contents 👊

Task

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

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

Explanation 

The diagram below depicts our assembled Sample Input:

We perform the following q=2 queries:

  1. 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.
  2. 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.

Solution - Variable Sized Arrays in C++

#include <iostream>
#include <vector>

using namespace std;

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

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

    for (int i = 0; i < n; i++) {
        int k;
        cin >> k;

        a[i].resize(k);
        for (int j = 0; j < k; j++) {
            cin >> a[i][j];
        }
    }

    for (int q_num = 0; q_num < q; q_num++) {
        int i, j;
        cin >> i >> j;
        cout << a[i][j] << endl;
    }

    return 0;
}

Disclaimer: The above Problem (Variable Sized Arrays) is generated by Hacker Rank but the Solution is Provided by Sloth Coders. This tutorial is only for Educational  and Learning Purpose.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment