Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Variable Sized Arrays solution in C++ programming

YASH PAL, 31 July 202416 January 2026

HackerRank C++ Variable-Sized Arrays solution – In this HackerRank Variable-Sized Arrays problem in C++ programming language. 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.

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.

HackerRank Variable Sized Arrays solution in c++ programming

HackerRank Variable-Sized Arrays problem solution in C++ programming.

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int n, queries;
    cin >> n >> queries;

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

    for (int i = 0; i < n; i++)
    {
        int length;
        cin >> length;
        arr[i].resize(length);
        for(int j = 0; j < length; j++)
        {
            cin >> arr[i][j];
        }       
    }

    for (int k = 0; k < queries; k++)
    {
        int i, j;
        cin >> i >> j;
        cout << arr[i][j]<<endl;
    }    
    return 0;
}

Second solution

#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    int n;
    int q;
    cin >> n >> q;
    
    // Create an array of pointers to integer arrays 
    // (i.e., an array of variable-length arrays)
    int** outer = new int*[n];

    // Fill each index of 'outer' with a variable-length array
    for(int i = 0; i < n; i++) {
        int k;
        cin >> k;
        // Create an array of length 'k' at index 'i'
        outer[i] = new int[k];

        // Fill each cell in the 'inner' variable-length array
        for(int j = 0; j < k; j++) {
            cin >> outer[i][j];
        }
    }

    // Perform queries:
    while(q-- > 0) {
        int outer_index;
        int inner_index;
        cin >> outer_index >> inner_index;
        
        // Find the variable-length array located at outer_index
        // and print the value of the element at inner_index.
        cout << outer[outer_index][inner_index] << endl;
    }

    return 0;
}

Third solution

#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <cassert>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <iostream>

#define pb push_back
#define all(x) (x).begin(), (x).end()

#ifdef KAZAR
  #define eprintf(...) fprintf(stderr,__VA_ARGS__)
#else
  #define eprintf(...) 0
#endif

using namespace std;

template<class T> inline void umax(T &a,T b){if(a < b) a = b;}
template<class T> inline void umin(T &a,T b){if(a > b) a = b;}
template<class T> inline T abs(T a){return a > 0 ? a : -a;}

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

const int inf = 1e9 + 143;
const ll longinf = 1e18 + 143;

inline int read(){int x;scanf(" %d",&x);return x;}

const int N = 123456;

int *a[N];

int main(){

#ifdef KAZAR
  freopen("f.input","r",stdin);
  freopen("f.output","w",stdout);
  freopen("error","w",stderr);
#endif

  int n = read();
  int q = read();

  assert(1 <= n && n <= 1e5);
  assert(1 <= q && q <= 1e5);

  for (int i = 0; i < n; i++) {
    int k = read();
    a[i] = new int[k];
    for (int j = 0; j < k; j++) {
      a[i][j] = read();
    }
  }

  for (int i = 0; i < q; i++) {
    int x = read();
    int y = read();
    printf("%dn", a[x][y]);
  }

  return 0;
}

Fourth solution

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

int main() {
    int *arr[100004], siz[100004];
    int n,q;
    int tot = 0;
    scanf("%d %d", &n, &q);
    assert(n >= 1 && n <= 100000 && q >= 1 && q <= 100000);
    for(int i = 0 ; i< n ; i++)
    {
        int num;
        scanf("%d", &num);
        assert(num >= 1 && num <= 300000);
        tot += num;
        siz[i] = num;
        arr[i] = (int*)malloc(num*sizeof(int));
        for(int j = 0 ; j < num; j++)
        {
            scanf("%d", &arr[i][j]);
            assert(arr[i][j] >= 0 && arr[i][j] <= 1000000);
        }
    }
    assert(tot >= 1 && tot <= 300000 && tot >= n);
    while(q--)
    {
        int a,b;
        scanf("%d %d", &a, &b);
        assert(a >= 0 && a < n && b >= 0 && b < siz[a]);
        printf("%dn", arr[a][b]);
    }
    return 0;
}
C++ Solutions coding problems solutions Hackerrank Problems Solutions cppHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank C++ Problems Solutions
Say “Hello, World!” with C++ solution
Input and Output solution
Basic Data Types solution
Conditional Statements solution
For loop solution
Functions solution
Pointer solution
Arrays Introduction solution
Variable-Sized Arrays solution
Attribute Parser solution
StringStream solution
Strings solution
Structs solution
Class solution
classes and objects solution
Box It! solution
Inherited code solution
Exceptional server solution
Virtual Functions solution
Abstract Classes — Polymorphism solution
Vector-Sort solution
Vector-Erase solution
Lower Bound-STL solution
Sets-STL solution
Maps-STL solution
Print Pretty solution
Deque-STL solution
Inheritance Introduction solution
Hotel Prices solution
Cpp exception handling solution
Rectangle Area solution
Multi-Level Inheritance solution
Overloading Ostream Operator solution
Messages Order solution
Accessing Inherited Functions solution
Magic Spells solution
C++ Class Templates problem solution
Preprocessor Solution
Operator Overloading solution
Overload Operators solution
Attending workshops solution
C++ Class Template Specialization solution
C++ Variadics problem solution
Bit Array solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes