Created
April 2, 2022 03:09
-
-
Save thanhpp/ebf2734a33d20e02b711dc30ebbbee68 to your computer and use it in GitHub Desktop.
kyber_test_idx
This file contains 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
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