cpp
Hackerrank Variable Sized Arrays Solution C Explained

HackerRank variable sized arrays solution c++ explained

Variable Sized Arrays, is a HackerRank problem from the Introduction subdomain of the C++ coding section. This article will explain how you can solve the ‘HackerRank variable sized arrays solution c++’ and explain it stepwise for your better understanding.

variable sized arrays solution c++

Problem statement

Consider an n-element array, where each index a in the array contains a reference to an array of integers where the value varies from array to array…

You can check out the complete problem on Variable sized Arrays (opens in a new tab) at HackerRank.

The below-given code snippet is the solution to the problem. Mind scrolling further below if you’re looking for an explanation of the following snippet.

Related: Top 5 tech skills that you can learn

Solution

Variable Sized Arrays

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
 
int main() {
    int x, y, s= 0;
    cin >> x >> y;
    int *arr[x];
    while(x--){
        int num;
        cin >> num;
        arr[s] = new int[num];
        for(int i=0; i<num; i++){
            cin>>arr[s][i];
        }
        s++;
    }
    while(y--){
        int a,b;
        cin >> a>>b;
        cout << arr[a][b]<< endl;
    }
    return 0;
}

IndGeek provides solutions in the software field, and is a hub for ultimate Tech Knowledge.