Variable Sized Arrays - Hackerrank Solution

 

Variable Sized Arrays


Objective:


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++.


Solution:

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

Note:

This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.

Disclaimer:-

The above hole problem statement is given by hackerrank.com, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You.

Variable Sized Arrays - Hackerrank Solution Variable Sized Arrays - Hackerrank Solution Reviewed by CodexRitik on October 16, 2020 Rating: 5

No comments:

Powered by Blogger.