C++ static array its due in 3 hours


olve the Variable Sized Array problem in hacker rank using Static instead of Dynamic Arrays. Of course, you should have already solved with Dynamic, but how can you get it to work with Static Arrays. Here is the code which I developed to start you out. You need to complete the input/output segment. I used a 1 Dimensional array to hold all the data and another 1 Dimensional array to tell me where each row starts. See if you can figure out how to do this.checl the code down how can you get it to work with Static Arrays

Don't use plagiarized sources. Get Your Custom Essay on
C++ static array its due in 3 hours
Just from $13/Page
Order Essay

This has direct application for unequal structures written to a binary file. THE CODE FOR DYNAMIC ARRAY IS DOWN AT THE END NEED IT CHANGE FROM DYNAMIC ARRAY TO STATIC ARRAY 

using namespace std;

int main() {
//Declare variables and arrays
const int MAX=500000;
const int ROW=300000;
int a[MAX]={};//1-D Array
int rc[ROW]={};//1-D Array where each row will start in the 1-D array above
int n,q,cnt,k;
//Read in the data
cin>>n>>q;//#Rows and #Queries
//Read in the entire array
cnt=0;//Start the array count at 0

orginal question

Consider an -element array, , where each index  in the array contains a reference to an array of  integers (where the value of  varies from array to array). See the Explanation section below for a diagram.

Given , you must answer  queries. Each query is in the format i j, where  denotes an index in array  and  denotes an index in the array located at . For each query, find and print the value of element  in the array at location  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  (the number of variable-length arrays) and  (the number of queries).
Each line  of the  subsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the -element array located at .
Each of the  subsequent lines contains two space-separated integers describing the respective values of  (an index in array ) and  (an index in the array referenced by ) for a query.


All indices in this challenge are zero-based.

All the given numbers are non negative and are not greater than

Output Format

For each pair of  and  values (i.e., for each query), print a single integer denoting the element located at index  of the array referenced by . There should be a total of  lines of output.

Sample Input

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

Sample Output



#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);



        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;



Calculate the price of your paper

Total price:$26
Our features

We've got everything to become your favourite writing service

Need a better grade?
We've got you covered.

Order your paper