SHARE
TWEET

Untitled

a guest Jul 20th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FIND PAIR WITH GIVEN SUM IN THE ARRAY
  2. // Go program to find the pair(s) with giving sum .
  3. //
  4. // Example: [8,7,2,5,3,1]
  5. // Given Sum: 10
  6. // Pairs: [8,2], [7,3]
  7.  
  8. package main
  9.  
  10. import (
  11.     "fmt"
  12. )
  13.  
  14. func findPair(arr []int, sum int) {
  15.     arrayCount := len(arr)
  16.     for i := 0; i < arrayCount; i++ {
  17.         for j := i + 1; j < arrayCount; j++ {
  18.             if arr[i]+arr[j] == sum {
  19.                 fmt.Printf("Found pair at [%v %v]\n", arr[i], arr[j])
  20.             }
  21.         }
  22.     }
  23. }
  24.  
  25. func main() {
  26.     givenSum := 10
  27.     array := []int{8, 7, 2, 5, 3, 1}
  28.  
  29.     fmt.Printf("Given sum: %v\n", givenSum)
  30.     findPair(array, givenSum)
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top