Created
June 22, 2018 23:32
-
-
Save arehmandev/bbf96a78828f0617deadf4f1ad188ae3 to your computer and use it in GitHub Desktop.
Solution to hackerrank dynamic array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.