We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
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>intmain(){intN;intQ;intt,x,y;intseq;intlastAnswer=0;int**seqList;int*seqCount;intindex;scanf("%i",&N);seqList=malloc(N*sizeof(int*));seqCount=malloc(N*sizeof(int));for(inti=0;i<N;i++)seqCount[i]=1;scanf("%i",&Q);for(inti=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(inti=0;i<N;i++)free(seqList[i]);free(seqList);free(seqCount);/* Enter your code here. Read input from STDIN. Print output to STDOUT */return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Dynamic Array
You are viewing a single comment's thread. Return to all 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.