Golang program to find if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach


In this Golang article, we are going to find if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach with iterative and optimized-iterative method. An array is a collection of elements of the same data type, arranged in a contiguous block of memory, and accessed using an index or a subscript.

Using Two Pointer Approach With Iterative Method

In this method, we will define a pairWithGivenSum() function using iterative approach that is used to find if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Now, create a pairWithGivenSum() function that takes an array of integers and a target sum value as input. This function will find if there exists a pair of numbers in an array that add up to a given target sum.

  • Step 3 − It uses two pointers, left and right to keep track of the start and end indices of the array.

  • Step 4 − It then moves the pointers inward by comparing the sum of the elements at those pointers with the target sum.

  • Step 5 − If the sum is less than the target sum, the left pointer moves to the right. If the sum is greater than the target sum, the right pointer moves to the left.

  • Step 6 − The function returns true if there exists a pair of elements that add up to the target sum, or false if no such pair exists.

  • Step 7 − Start the main() function. Inside the main() function, initialize an array and provide integer target sum value.

  • Step 8 − Now, call the pairWithGivenSum() function and pass the array & sum to it as argument.

  • Step 9 − Further, the result if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach is printed using the fmt.Println() function.

Example

The Following Is The Go Language Program To Find If There Exists A Pair Of Numbers In An Array That Add Up To A Given Target Sum Using Two Pointer Approach With Iterative Method

package main

import "fmt"

func pairWithGivenSum(arr []int, targetSum int) bool {
   left, right := 0, len(arr)-1

   for left < right {
      sum := arr[left] + arr[right]
      if sum == targetSum {
         return true
      } else if sum < targetSum {
         left++
      } else {
         right--
      }
   }
   return false
}

func main() {
   arr := []int{10, 40, 30, 80, 50}
   targetSum := 70

   if pairWithGivenSum(arr, targetSum) {
      fmt.Println("There exists a pair of numbers in the array that add up to sum", targetSum)
   } else {
      fmt.Println("There does not exist a pair of numbers in the array that add up to sum", targetSum)
   }
}

Output

There exists a pair of numbers in the array that add up to sum 70

Using Two Pointer Approach With Optimized Iterative Method

In this method, we will define a pairWithGivenSum() function using iterative approach in optimized manner that is used to find if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Now, create a pairWithGivenSum() function that takes an array of integers and a target sum value as input. This function will find if there exists a pair of numbers in an array that add up to a given target sum and returns the boolean value.

  • Step 3 − It initializes two pointers left and right at the beginning and the end of the array.

  • Step 4 − Then, compare the sum of the elements at the left and right pointers with the target sum. If the sum is less than the target sum, it increments the left pointer. If the sum is greater than the target sum, it decrements the right pointer.

  • Step 5 − The function continues indicating that no pair of elements exists that add up to the target sum, or until it finds a pair of elements that add up to the target sum and returns true.

  • Step 6 − Start the main() function. Inside the main() function, initialize an array and provide integer target sum value.

  • Step 7 − Now, call the pairWithGivenSum() function and pass the array & sum to it as argument.

  • Step 8 − Further, the result if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach is printed using the fmt.Println() function.

Example

Following Is Go Language Program To Find If There Exists A Pair Of Numbers In An Array That Add Up To A Given Target Sum Using Two Pointer Approach With Optimized Iterative Method

package main

import "fmt"

func pairWithGivenSum(arr []int, targetSum int) bool {
   var left, right int
   for left < right {
      if arr[left]+arr[right] == targetSum {
         return true
      } else if arr[left]+arr[right] < targetSum {
         left++
      } else {
         right--
      }
   }
   return false
}

func main() {
   arr := []int{10, 40, 30, 80, 50}
   targetSum := 7

   if pairWithGivenSum(arr, targetSum) {
      fmt.Printf("There exists a pair of numbers in the array that add up to %d\n", targetSum)
   } else {
      fmt.Printf("There does not exist a pair of numbers in the array that add up to %d\n", targetSum)
   }
}

Output

There does not exists a pair of numbers in the array that add up to 7

Conclusion

We have successfully compiled and executed a go language program to find if there exists a pair of numbers in an array that add up to a given target sum using two pointer approach with iterative and optimized-iterative method along with two examples. In the first example, we have used the iterative method and in the second example, we have used the optimized-iterative method.

Updated on: 03-Apr-2023

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements