Welcome To Golang By Example

Find all triplets in an array that adds to a target number in Go (Golang)

Table of Contents

Overview

Let’s say the input is

[1, 2, 3, 4, 1]

Then the answer would be

[[1 2 3] [1 4 1] [2 3 1]]

We can use a hash for the solution. It is based upon the idea that

Program

package main

import (
	"fmt"
)

func main() {
	output := threeSumTarget([]int{1, 2, 3, 4, 1}, 6)
	fmt.Println(output)
}

func threeSumTarget(nums []int, target int) [][]int {

	numsLength := len(nums)
	var results [][]int
	for k := 0; k < numsLength-2; k++ {
		numsMap := make(map[int]int)
		for i := k + 1; i < numsLength; i++ {
			if numsMap[target-(nums[i]+nums[k])] > 0 {
				result := []int{nums[k], target - (nums[i] + nums[k]), nums[i]}
				results = append(results, result)
			}
			numsMap[nums[i]] = i
		}
	}

	return results
}

Output

[[1 2 3] [1 4 1] [2 3 1]]