Golang program to find the maximum product of two numbers in an array using two pointer approach


In this Golang article, we are going to find the maximum product of two numbers in an array 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 productMax() function using iterative approach that is used to find the maximum product of two numbers in an array using two pointer approach.

Algorithm

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

  • Step 2 − Now, create a productMax() function that takes an array of integers. This function will return an integer value giving the maximum product of two numbers in an array.

  • Step 3 − This function firstly sorts the array and initializes two pointers as left and right. The left pointer points to the first element of the sorted array while the right pointer points to the last element of the sorted array.

  • Step 4 − In every iteration, the left and right pointer numbers are multiplied and stores their product in a variable called prod. And if the prod value is greater than the prodMax, then value of prodMax is updated.

  • Step 5 − Start the main() function. Inside the main() function, initialize the array.

  • Step 6 − Now, calls the productMax() function and pass the array to it.

  • Step 7 − Further, the resultant maximum product value is printed using the fmt.Println() function.

Example

Following is a golang program to find the maximum product of two numbers in an array using two pointer approach with iterative method

package main

import (
   "fmt"
   "sort"
)

func productMax(nums []int) int {
   n := len(nums)
   sort.Ints(nums)
   i, j := 0, n-1
   prodMax := nums[i] * nums[j]
   for i < j {
      prod := nums[i] * nums[j]
      if prod > prodMax {
         prodMax = prod
      }
      if nums[i] < 0 {
         i++
      } else {
         j--
      }
   }
   return prodMax
}

func main() {
   nums := []int{20, 10, 30, 60, 10}
   fmt.Println("Array:", nums)
   fmt.Println("Maximum product:", productMax(nums))
}

Output

Array: [20 10 30 60 10]
Maximum product: 600

Using Two Pointer Approach With Optimized Iterative Method

In this method,, we will define a findMaxProduct() function using iterative approach in optimized manner that is used to find the maximum product of two numbers in an array using two pointer approach.

Algorithm

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

  • Step 2 − Start the main() function. Inside the main() function, initialize the array.

  • Step 3 − Now, calls the findMaxProduct() function and pass the array to it.

  • Step 4 − Further, the resultant maximum product value is printed using the fmt.Println() function.

  • Step 5 − Now, create a findMaxProduct() function that takes an array of integers. This function will return an integer value giving the maximum product of two numbers in an array.

  • Step 6 − Firstly, it checks if the length of the array is less than 2. If so, it returns 0 since it cannot find the product of two numbers in an array of length less than 2. Otherwise, the array is sorted.

  • Step 7 − Now, initialize two pointers as left and right. The left pointer points to the first element of the sorted array while the right pointer points to the last element of the sorted array.

  • Step 8 − In every iteration, the left and right pointer numbers are multiplied and stores their product in a variable called product. And if the product value is greater than the maxProduct, then value of maxProduct is updated.

Example

Following is go language program to find the maximum product of two numbers in an array using two pointer approach with optimized iterative method

package main

import (
   "fmt"
   "sort"
)

func main() {
   arr := []int{50, 20, 10, 40, 60}
   maxProduct := findMaxProduct(arr)
   fmt.Println("Maximum product of two numbers:", maxProduct)
}

func findMaxProduct(arr []int) int {
   n := len(arr)
   if n < 2 {
      return 0
   }
   sort.Ints(arr)
   left, right := 0, n-1
   maxProduct := 0
   for left < right {
      product := arr[left] * arr[right]
      if product > maxProduct {
         maxProduct = product
      }
		if arr[left] < arr[right] {
         left++
      } else {
         right--
      }
   }
   return maxProduct
}

Output

Maximum product of two numbers: 3000

Conclusion

We have successfully compiled and executed a go language program to find the maximum product of two numbers in an array using two pointer approach 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

239 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements