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.
Space complexity: O(n), if deallocates dynamic array
intcompute_idx(intx,intlast_answer,intn){return(x^last_answer)%n;}int*dynamicArray(intn,intqueries_rows,intqueries_columns,int**queries,int*result_count){*result_count=0;int*ans_arr=(int*)malloc(queries_rows*sizeof(int));// initialize 2-dimensional arrayint**res_arr=(int**)malloc(n*sizeof(int*));for(inti=0;i<n;i++){res_arr[i]=(int*)malloc(1000*sizeof(int));}int*res_idx=(int*)calloc(n,sizeof(int));intlast_answer=0;for(inti=0;i<queries_rows;i++){inttemp_idx=compute_idx(queries[i][1],last_answer,n);if(queries[i][0]==1){res_arr[temp_idx][res_idx[temp_idx]++]=queries[i][2];}else{last_answer=res_arr[temp_idx][queries[i][2]%res_idx[temp_idx]];ans_arr[*result_count]=last_answer;(*result_count)++;}}// Free memory for res_arr and res_idxfor(inti=0;i<n;i++){free(res_arr[i]);}free(res_arr);free(res_idx);returnans_arr;}
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 →
Time complexity: O(n)
Space complexity: O(n), if deallocates dynamic array