File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ package arrays
2+
3+ import "fmt"
4+
5+ /*
6+ * Complete the 'dynamicArray' function below.
7+ * 2 2
8+ * 1 x y
9+ * 2 x y
10+ *
11+ * The function is expected to return an INTEGER_ARRAY.
12+ * The function accepts following parameters:
13+ * 1. INTEGER n
14+ * 2. 2D_INTEGER_ARRAY queries
15+ */
16+
17+ func DynamicArray (n int32 , queries [][]int32 ) []int32 {
18+ // initialize inputs and output
19+ var result []int32
20+ arr := make ([][]int32 , n )
21+ numQueries := len (queries )
22+ lastAnswer := int32 (0 )
23+
24+ // iterate each query
25+ for i := 0 ; i < numQueries ; i ++ {
26+ x := queries [i ][1 ]
27+ y := queries [i ][2 ]
28+
29+ // query type 1
30+ if queries [i ][0 ] == 1 {
31+ idx := (x ^ lastAnswer ) % n
32+ arr [idx ] = append (arr [idx ], y )
33+ }
34+
35+ // query type 2
36+ if queries [i ][0 ] == 2 {
37+ idx := (x ^ lastAnswer ) % n
38+ lastAnswer = arr [idx ][y % int32 (len (arr [idx ]))]
39+ result = append (result , lastAnswer )
40+ fmt .Println (lastAnswer )
41+ }
42+ }
43+
44+ return result
45+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ func main () {
4+ //test each implementation
5+ }
You can’t perform that action at this time.
0 commit comments