Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

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

Posted on August 18, 2023August 18, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

Let’s say the input is

[-3, 0, 3, 4, -1, -6]

Then the answer would be

[[-3 0 3] [-3 4 -1]]

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

  • If two of the numbers are x and y
  • Then the other number will be -(x+y)

Program

package main
import (
"fmt"
)
func main() {
output := threeSumZero([]int{-3, 0, 3, 4, -1, -6})
fmt.Println(output)
}
func threeSumZero(nums []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[0-(nums[i]+nums[k])] > 0 {
result := []int{nums[k], 0 - (nums[i] + nums[k]), nums[i]}
results = append(results, result)
}
numsMap[nums[i]] = i
}
}
return results
}

Output

[[-3 0 3] [-3 4 -1]]

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP