Variable Sized Arrays in C++ hackerrank solution

Problem statement:

Given an array of N integer arrays, where the size of each array may vary, you need to answer Q queries. Each query consists of two integers, i and j. You need to print the element located at the i-th index of the j-th array.

Input Format:

The first line contains two space-separated integers, N (the number of arrays) and Q (the number of queries).

The next N lines describe the arrays. Each line starts with an integer, K, followed by K space-separated integers.

The next Q lines contain two space-separated integers, i and j, representing the i-th index of the j-th array to query.

      
  

Sample Input:

2 2 3 1 5 4 5 1 2 8 9 3 0 1 1 3
      

Sample Output:

5 9

Explanation:

Explanation:

In this example, we have N = 2 arrays and Q = 2 queries.

The first array has 3 elements: [1, 5, 4].

The second array has 5 elements: [1, 2, 8, 9, 3].

For the first query (0 1), we need to print the element at index 0 of the second array. The element is 1, so we print it as the output.

For the second query (1 3), we need to print the element at index 1 of the third array. Since the third array doesn't exist (as it is 0-based), we consider the second array. The element at index 1 of the second array is 9, so we print it as the output.

      
  #include <iostream>
  #include <vector>
  
  using namespace std;
  
  int main() {
      int n, q;
      cin >> n >> q;
  
      // Create a vector of vectors to store the arrays
      vector> arrays(n);
  
      // Read the input arrays
      for (int i = 0; i < n; i++) {
          int k;
          cin >> k;
          arrays[i].resize(k);
          for (int j = 0; j < k; j++) {
              cin >> arrays[i][j];
          }
      }
  
      // Process the queries
      for (int i = 0; i < q; i++) {
          int a, b;
          cin >> a >> b;
          cout << arrays[a][b] << endl;
      }
  
      return 0;
  }
  

Code Explanation

Explanation of Solution:

We start by reading the values of N and Q from the input, representing the number of arrays and queries, respectively.

We create a vector of vectors named arrays to store the arrays. Each element of the arrays vector represents an individual array.

We then iterate N times and read each array from the input. For each array, we first read the size K and resize the vector to accommodate K elements. Then, we read K integers representing the elements of the array.

After reading all the arrays, we proceed to process the queries. We iterate Q times and read the values of i and j for each query.

Finally, we output the element located at the i-th index of the j-th array by accessing arrays[a][b].

The program reads the input, stores the arrays in the vector of vectors, and then processes the queries. For each query, it prints the requested element from the corresponding array.

The example input provided earlier results in the output: 5 and 9, which matches the expected output.

Note: The solution assumes that the input is valid and follows the given format. It doesn't include extensive error handling or input validation for brevity.