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
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
#include
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.
Constraints
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
5
9
CODE
#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;
}
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more