- Prepare
- Data Structures
- Arrays
- Dynamic Array
- Discussions

# Dynamic Array

# Dynamic Array

+ 25 comments BOOOOM! That was challenging and fun. I wish the language of the challenge were easier to understand. For example, the lastAn should have been named "lastAnswer" so as to be easier to understand. I thought lastAn was some kind of mathematical term. I would recommend the person responsible for making these challenges read "Clean Code" by Uncle Bob Martin!

+ 1 comment Question is so confusing. Cloud you please re-frame question?

+ 2 comments Hackerrank,

Please have readable question libraries. You questions very poorly written. It needs more effort in undertanding the questions than solving the questions.

Please improve the question descriptions.

+ 1 comment Apologies for any offence caused to the author/writer of the quiz, it's just, I would be really grateful if someone could help me decipher what the question is asking for?

I've read the questions about 5 times, and am struggling to understand what it is that is needed to be done?

(I'll keep re-reading in the meantime, but if someone could help in the meantime that would be grand :( )

+ 8 comments I will try to clarify the question for people which are having a hard time to understand it, like I was: seqList is basically an Anrray of Arrays, each Array in seqList will be dynamic, while seqList itself will be static with the input 'N' determining the number of Arrays that will compose it. The 'Q' input will determine the number of Queries. Each Query will demand an input of 3 integers: 't', 'x' and 'y'. The 't' is for "type". The type 1 Query will append the 'y' variable to some array in seqList, the formula will specify which one. The type 2 Query will take some value, that was alredy assigned to some Array of the seqList, and copy that to lastAnswer. The first formula specifies the array of seqList and the second formula specifies which value of that Array must be taken. I hope the schematic below make it clearer.

setList List 0 [1 : 2 : 3] List 1 [4 : 5] List 2 [] List 3 [6 : 7 : 8 : 9] . . List N-1 [0: 1]

`It might be a little bit harder for C programmers because it will be needed to reallocate memory dinamically for each type 1 query. I think it's easier if you keep in mind that the seqList is just a pointer to a pointer to a integer. And it's necessary to crate array to store the number of elements in each array of seqList. I hope this post might be useful for someone. Please let me know if it's not clear enough or if it's not clear at all. This is my solution in c if anyone is interested.`

//Guilherme Benner Martelli #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int N; int Q; int t, x, y; int seq; int lastAnswer = 0; int **seqList; int * seqCount; int index; scanf("%i", &N); seqList = malloc(N*sizeof(int *)); seqCount = malloc(N*sizeof(int)); for(int i = 0; i < N; i++) seqCount[i] = 1; scanf("%i", &Q); for(int i = 0; i < Q; i++){ scanf("%i %i %i", &t, &x, &y); if(t==1){ seq = ((x^lastAnswer)%N); seqList[seq] = realloc(seqList[seq],seqCount[seq]*sizeof(int)); seqList[seq][seqCount[seq]-1] = y; seqCount[seq]++; }else { seq = ((x^lastAnswer)%N); index = y%(seqCount[seq]-1); lastAnswer = seqList[seq][index]; printf("%i\n", lastAnswer); } } for(int i = 0; i < N; i++) free(seqList[i]); free(seqList); free(seqCount); /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }

Sort 1756 Discussions, By:

Please Login in order to post a comment