Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. // PRINT ALL SUB-ARRAYS WITH 0 SUM
  2. // Go program to find zero sum in a given array
  3. //
  4. // Example: [4,2,-3,-1,0,4]
  5. // 0 Sum Pairs:
  6. // [3,4,-7]
  7. // [4,-7,3]
  8. // [-7,3,1,3]
  9. // [3,1,-4]
  10. // [3,1,3,1,-4,-2,-2]
  11. // [3,4,-7,3,1,3,1,-4,-2,-2]
  12.  
  13. package main
  14.  
  15. import (
  16. "fmt"
  17. )
  18.  
  19. func findZeroSum(arr []int) {
  20. arrayCount := len(arr)
  21. sum := 0
  22.  
  23. for i := 0; i < arrayCount; i++ {
  24.  
  25. sum = 0
  26. for j := 0; j < arrayCount; j++ {
  27. sum += arr[j]
  28. if sum == 0 {
  29. fmt.Printf("Found pair at: [%v %v]\n", arr[i], arr[j])
  30. }
  31.  
  32. }
  33. }
  34. }
  35.  
  36. func main() {
  37. array := []int{3, 4, -7, 3, 1, 3, 1, -4, -2, -2}
  38. fmt.Printf("Given arry: %v\n", array)
  39.  
  40. findZeroSum(array)
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement