- package main
- import "fmt"
- // Given an array of Integers, create a function to tell whether there is a pair of any integers that summed up to Q.
- // - The function should receive an array of integers and integer Q
- // - The function shall return `true` if there is a pair that summed up to Q, return `false` otherwise
- // Example
- // arr: []int{1, 2, 4, 7}
- // when Q == 3 then true
- // when Q == 4 then false
- // when Q == 5 then true
- // when Q == 6 then true
- // when Q == 7 then false
- // when Q == 8 then true
- // when Q == 9 then true
- // when Q == 10 then false
- func main() {
- arr := []int{1, 2, 4, 7}
- q := []int{3, 4, 5, 6, 7, 8, 9, 10}
- for _, i := range q {
- havePair := solution(arr, i)
- fmt.Printf("when Q == %d then %t\n", i, havePair)
- }
- }
- func solution(arr []int, q int) bool {
- // Your solution here
- temp := 0
- ok := true
- for idx := range arr {
- temp += arr[idx]
- if temp == q {
- ok = true
- temp = 0
- break
- } else {
- ok = false
- }
- }
- if !ok {
- return false
- } else {
- return true
- }
- }
Raw Paste