Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement