Skip to content

Instantly share code, notes, and snippets.

@arehmandev
Created June 22, 2018 23:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arehmandev/bbf96a78828f0617deadf4f1ad188ae3 to your computer and use it in GitHub Desktop.
Save arehmandev/bbf96a78828f0617deadf4f1ad188ae3 to your computer and use it in GitHub Desktop.
Solution to hackerrank dynamic array
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
var lastAnswer int32
/*
* Complete the dynamicArray function below.
*/
func dynamicArray(N int32, queries [][]int32) []int32 {
var sequenceOne []int32
var sequenceTwo []int32
var useSequence = [][]int32{sequenceOne, sequenceTwo}
var returnSeq []int32
for _, query := range queries {
queryType := query[0]
x := query[1]
y := query[2]
// fmt.Println(x,"^",lastAnswer, "%", N)
sequenceNum := (x^lastAnswer) % N
// fmt.Println("SequenceNum:", sequenceNum)
if queryType == 1 {
// fmt.Println("RUNNING QUERY ONE")
useSequence[sequenceNum] = append(useSequence[sequenceNum], y)
}
if queryType == 2 {
// fmt.Println("RUNNING QUERY TWO")
elementIndex := y % int32(len(useSequence[sequenceNum]))
lastAnswer = useSequence[sequenceNum][elementIndex]
returnSeq = append(returnSeq, lastAnswer)
// fmt.Println("Last answer:", lastAnswer)
}
}
// fmt.Println(useSequence)
return returnSeq
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 1024 * 1024)
nq := strings.Split(readLine(reader), " ")
nTemp, err := strconv.ParseInt(nq[0], 10, 64)
checkError(err)
n := int32(nTemp)
qTemp, err := strconv.ParseInt(nq[1], 10, 64)
checkError(err)
q := int32(qTemp)
var queries [][]int32
for queriesRowItr := 0; queriesRowItr < int(q); queriesRowItr++ {
queriesRowTemp := strings.Split(readLine(reader), " ")
var queriesRow []int32
for _, queriesRowItem := range queriesRowTemp {
queriesItemTemp, err := strconv.ParseInt(queriesRowItem, 10, 64)
checkError(err)
queriesItem := int32(queriesItemTemp)
queriesRow = append(queriesRow, queriesItem)
}
if len(queriesRow) != int(3) {
panic("Bad input")
}
queries = append(queries, queriesRow)
}
result := dynamicArray(n, queries)
for resultItr, resultItem := range result {
fmt.Fprintf(writer, "%d", resultItem)
if resultItr != len(result) - 1 {
fmt.Fprintf(writer, "\n")
}
}
fmt.Fprintf(writer, "\n")
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
@grantbow
Copy link

grantbow commented May 5, 2022

Thanks, mostly works OK, but the above version works only for hackerrank's stated sample input where N is two. useSequence needs size N. Looks like you simply didn't check in the version that passed all the tests. In my version I used slightly different variable names. I replaced your lines 20-23 with code similar to below and then it passed.

useSequence := [][]int32{}
for i:=int32(0); i<N; i++ {
    useSequence = append(useSequence, []int32{})
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment