Question: Solve the Variable Sized Array problem in hacker rank using Static instead of Dynamic Arrays. Of course, you should have already solved with Dynamic, but
Solve 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.
#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 but how can you get it to work with Static Arrays
#include
#define pb push_back #define all(x) (x).begin(), (x).end()
#ifdef KAZAR #define eprintf(...) fprintf(stderr,__VA_ARGS__) #else #define eprintf(...) 0 #endif
using namespace std;
template
typedef long long ll; typedef pair
const int inf = 1e9 + 143; const ll longinf = 1e18 + 143;
inline int read(){int x;scanf(" %d",&x);return x;}
const int N = 123456;
int *a[N];
int main(){
#ifdef KAZAR freopen("f.input","r",stdin); freopen("f.output","w",stdout); freopen("error","w",stderr); #endif
int n = read(); int q = read();
assert(1 <= n && n <= 1e5); assert(1 <= q && q <= 1e5);
for (int i = 0; i < n; i++) { int k = read(); a[i] = new int[k]; for (int j = 0; j < k; j++) { a[i][j] = read(); } }
for (int i = 0; i < q; i++) { int x = read(); int y = read(); printf("%d ", a[x][y]); }
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
