HackerRank Dynamic Array Problem Solution Yashwant Parihar, March 18, 2025March 18, 2025 In this post, we will solve the Dynamic Array Problem in HackerRank. Declare a 2-dimensional array, arr, with n empty arrays, all zero-indexed. Declare an integer, last Answer, and initialize it to 0.You need to process two types of queries: Query: 1 x y Compute idx = (xlast Answer). Append the integer y to arr[idx]. Query: 2 x y Compute idx = (xlast Answer). Set last Answer = arr[idx][y%size(arr[idx])]. Store the new value of the last Answer in an answers array. Function DescriptionComplete the dynamicArray function with the following parameters: int n: the number of empty arrays to initialize in arr int queries [q][3]: 2-D array of integers Returnsint: the results of each type 2 query in the order they are presented Input FormatThe first line contains two space-separated integers, n, the size of arr to create, and q. the number of queries, respectively. Each of the q subsequent lines contains a query string. queries[I]. Constraints 1≤n, q≤105 0 < x, y ≤ 109 It is guaranteed that query type 2 will never query an empty array or index. Sample Input STDIN Function ----- -------- 2 5 size of arr[] n = 2, size of queries[] q = 5 1 0 5 queries = [[1,0,5],[1,1,7],[1,0,3],[2,1,0],[2,1,1]] 1 1 7 1 0 3 2 1 0 2 1 1 Sample Output 7 3 HackerRank Dynamic Array Solution in C #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> struct sequence { int *data; int len; int cap; }; struct sequence *seqList; void push_list(int item, struct sequence *n) { if (n->len >= n->cap) { int new_size = n->cap == 0 ? 1 : 2*n->cap; n->data=(int*)realloc(n->data,new_size); n->cap=new_size; } n->data[n->len++]=item; } void print_list(struct sequence *n) { int i; for (i=0; i<n->len; i++) printf("%d ",n->data[i]); putchar('\n'); } int main() { int N, Q, i; int query, x, y; int lastAns =0; struct sequence *chosen; /* Enter your code here. Read input from STDIN. Print output to STDOUT */ scanf("%d %d", &N, &Q); seqList = (struct sequence *) malloc(N * sizeof(struct sequence)); for (i=0; i<N; i++) { seqList[i].data = NULL; seqList[i].len = 0; seqList[i].cap = 0; } for (i=0; i<Q; i++) { scanf("%d %d %d", &query, &x, &y); chosen = &seqList[(x^lastAns)%N]; if (query==1) { push_list(y, chosen); } else if (query == 2) { lastAns = chosen->data[y%chosen->len]; printf("%d\n",lastAns); } } return 0; } HackerRank Dynamic Array Solution in C++ #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 = 0; int Q = 0; cin >> N >> Q; vector<int>* sequences = new vector<int>[N]; int lastans = 0; for (int i=0; i<Q; i++) { int command_type = 0; int x = 0; int y = 0; cin >> command_type >> x >> y; if (command_type == 1) { sequences[(x^lastans) % N].push_back(y); } else if (command_type == 2) { lastans = sequences[(x^lastans) % N][y % sequences[(x^lastans) % N].size()]; cout << lastans << "\n"; } } return 0; } HackerRank Dynamic Array Solution in Java import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int Q = in.nextInt(); ArrayList<ArrayList<Integer>> arrays = new ArrayList<ArrayList<Integer>>(N); for (int i = 0; i < N; ++i) { arrays.add(new ArrayList<Integer>()); } int lastans = 0; for (int i = 0; i < Q; ++i) { int operation = in.nextInt(); int x = in.nextInt(); int y = in.nextInt(); int idx = (x ^ lastans) % N; ArrayList<Integer> array = arrays.get(idx); if (operation == 1) { // "1 x y" - Insert y at the end of the ((x⊕lastans) mod N)th sequence. array.add(y); } else { // "2 x y" - Print the value of the (y mod size)th element of the ((x⊕lastans) mod N)th sequence. Here, size denotes the size of the related sequence. Then, assign this integer to lastans. lastans = array.get(y % array.size()); System.out.println(lastans); } } } } HackerRank Dynamic Array Solution in JavaScript function processData(input) { let queries = input.split("\n"); let [n, q] = queries.shift().split(" ").map(Number); let lastAnswer = 0; let arrOfArr = []; for(let i = 0; i < q; i++) { let [t, x, y] = queries[i].split(" ").map(Number); let index = ((x ^ lastAnswer) % n); if(t === 1) { if(typeof arrOfArr[index] === "undefined") { arrOfArr[index] = []; } arrOfArr[index].push(y); } else if(t === 2) { lastAnswer = arrOfArr[index][y % arrOfArr[index].length]; console.log(lastAnswer); } } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); HackerRank Dynamic Array Solution in Python # Enter your code here. Read input from STDIN. Print output to STDOUT seqList = [] lastAns = 0 N,Q = map(int,raw_input().split()) for i in range(N): seqList.append([]) for i in range(Q): query = map(int,raw_input().split()) cmd = query[0] if cmd == 1: seqList[(query[1]^lastAns) % N].append(query[2]) elif cmd == 2: seq = seqList[ (query[1]^lastAns) % N] lastAns = seq[query[2] % len(seq)] print lastAns HackerRank Solutions Hackerrank Solutions