Variable Sized Arrays in C++ - Hacker Rank Solution


Variable Sized Arrays in C++ - Hacker Rank Solution
Variable Sized Arrays in C++ - Hacker Rank Solution


Problem


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.

Click here to know more about how to create variable sized arrays in C++.



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 10^6


Output Format

For each pair of i and j values (i.e., for each query), print a single integer denoting 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:

Variable Sized Arrays in C++ - Hacker Rank Solution
Variable-Length Array

We perform the following q = 2 queries:
  1. Find the array located at index i =0 , which corresponds to a[0] = [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[1]=[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 :-


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define MAX 1000
using namespace std;


int main() 
{
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int arr1[MAX][MAX],size[MAX];
    int n,q,s,pos1,pos2;
    cout<<"Enter a Number of Array";
    cin>>n>>q; 
    for(int i=0;i<n;i++)
    {
       cout<<"Enter a size of array";
       cin>>s;
       for(int j=0;j<s;j++)
       {
            cin>>arr1[i][j];
       } 
        
    }
    for(int i=0;i<q;i++)
    {
        cout<<"Enter a position";
        cin>>pos1>>pos2;
        cout<<arr1[pos1][pos2]<<"\n";
    }
    return 0;
}

Note : if the above code is not work them run following code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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;
    int q;
    cin >> n >> q;
    vector<int> a[n];
    for(int i = 0; i < n; i++){
        int m;
        cin >> m;
        int o;
        for(int j = 0; j < m; j++){
            cin >> o;
            a[i].push_back(o);
        }
    }
    
    int r, s;
    for(int k = 1; k <= q; k++){
        cin >> r >> s;
        cout << a[r][s] << endl;
    }
    return 0;
}





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
2 Comments
  • Unknown
    Unknown Monday, January 03, 2022

    this code is working but all test cases are not running only 2 test cases are running.

  • Rairohit_13
    Rairohit_13 Friday, January 28, 2022

    Thanks for the code.
    I was little bit confused about the solution but now everything is clear.

Add Comment
comment url