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.
Took me a while to understand the problem and the input description. Here is the code in golang:
varlastAnswerint32lastAnswer=0varanswers[]int32// you need to use the n parameter to generate the size of arrarr:=make([][]int32,n)// range the queries paramsfori:=0;i<len(queries);i++{// the first index (0) of the queries will always be the type of query// example {1 0 3}// 1 is the queryType, 0 is the x and 3 is the yqueryType:=queries[i][0]// the x will be the index (1)x:=queries[i][1]// and the y will be de value or index (2)y:=queries[i][2]// this is the formula given by the problem to get the index to appendidx:=((x^lastAnswer)%n)ifqueryType==1{arr[idx]=append(arr[idx],y)}ifqueryType==2{lastAnswer=arr[idx][y%int32(len(arr[idx]))]answers=append(answers,lastAnswer)}}returnanswers
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 →
Took me a while to understand the problem and the input description. Here is the code in golang: