Recent Post

Variable Sized Array.cpp-HackerRank

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

Given , you must answer  queries. Each query is in the format i j, where  denotes an index in array  and  denotes an index in the array located at . For each query, find and print the value of element  in the array at location  on a new line.


Solution-
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() 
{   
        int n, q;
    cin >> n >> q;
    int **arr = new int*[n];
    for ( int i = 0, k ; i < n ; ++ i )
    {
        cin >> k;
        arr[i] = new int[k];
        for ( int j = 0 ; j < k ; ++ j )
        {
            cin >> arr[i][j];
        }
    }
    for ( int i = 0, a, b ; i < q ; ++ i )
    {
        cin >> a >> b;   
        cout << arr[a][b] << endl;
    }
}

Link to the problem:-

Result!

Comments

Popular posts from this blog

Caesar Cipher.c-HackerRank

Bon Appétit.c-HackerRank

Electronics Shop.c-HackerRank