Skip to content

Instantly share code, notes, and snippets.

@thanhpp
Created April 2, 2022 03:09
Show Gist options
  • Save thanhpp/ebf2734a33d20e02b711dc30ebbbee68 to your computer and use it in GitHub Desktop.
Save thanhpp/ebf2734a33d20e02b711dc30ebbbee68 to your computer and use it in GitHub Desktop.
kyber_test_idx
func check(j, arrLength int) bool {
return j < (arrLength-1)
}
func Query1(arr []int, queries [][2]int) {
var queryResult int
for _, q := range queries {
if check(q[1], len(arr)) {
log.Fatalf("Invalid query: %d, %d", q[0], q[1])
}
for i := q[0]; i <= q[1]; i++ {
queryResult += arr[i]
}
fmt.Printf("Query %d, %d result %d", q[0], q[1], queryResult)
queryResult = 0
}
}
func Query2(arr []int, query [][2]int) {
if len(arr) < 1 {
log.Fatalf("empty input array")
}
var sumArr = make([]int, len(arr))
sumArr[0] = arr[0]
for i := 1; i < len(arr); i++ {
sumArr[i] = sumArr[i-1] + arr[i]
}
for _, q := range queries {
if check(q[1], len(arr)) {
log.Fatalf("Invalid query: %d, %d", q[0], q[1])
}
fmt.Printf("Query %d, %d result %d", q[0], q[1], sumArr[j] - sumArr[i] + arr[i])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment