• + 0 comments

    Took me a while to understand the problem and the input description. Here is the code in golang:

        var lastAnswer int32
        lastAnswer = 0
        var answers []int32
    		
    // you need to use the n parameter to generate the size of arr
        arr := make([][]int32, n)
    
    // range the queries params
        for i := 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 y
            queryType := 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 append
            idx := ((x ^ lastAnswer) % n)
    
            if queryType == 1 {
                arr[idx] = append(arr[idx], y)
            }
    
            if queryType == 2 {
                lastAnswer = arr[idx][y%int32(len(arr[idx]))]
                answers = append(answers, lastAnswer)
            }
    				
    
        }
    
        return answers