Skip to content

Commit a76fb34

Browse files
Lairon Acosta GuardiasLairon Acosta Guardias
authored andcommitted
adding the solution of the dinamic array problem
1 parent 1e1812c commit a76fb34

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func main() {
4+
//test each implementation
5+
}

0 commit comments

Comments
 (0)