Skip to content

Commit

Permalink
Challenge1_Quadruple_Sum_to_Target solution.go
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiwei-Feng committed Mar 12, 2021
1 parent e566694 commit bcdf30e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package Challenge1_Quadruple_Sum_to_Target

import (
"sort"
)

/*
Given an array of unsorted numbers and a target number,
find all unique quadruplets in it, whose sum is equal to the target number.
Input: [4, 1, 2, -1, 1, -3], target=1
Output: [-3, -1, 1, 4], [-3, 1, 1, 2]
Explanation: Both the quadruplets add up to the target.
Input: [2, 0, -1, 1, -2, 2], target=2
Output: [-2, 0, 2, 2], [-1, 0, 1, 2]
Explanation: Both the quadruplets add up to the target.
*/

func searchQuadruplets(arr []int, target int) [][4]int {
sort.Ints(arr)
quadruplets := make([][4]int, 0, 10)
for i := 0; i < len(arr)-3; i++ {
if i > 0 && arr[i] == arr[i-1] {
continue
}

for j := i + 1; j < len(arr)-2; j++ {
if j > i+1 && arr[j] == arr[j-1] {
continue
}
quadruplets = searchPair(arr, j+1, target, arr[i], arr[j], quadruplets)
}
}
return quadruplets
}

func searchPair(arr []int, left int, target int, first, second int, quadruplets [][4]int) [][4]int {
// assume arr is sorted
var (
i, j = left, len(arr) - 1
)
for i < j {
current := arr[i] + arr[j] + first + second
if current == target {
quadruplets = append(quadruplets, [4]int{first, second, arr[i], arr[j]})
i++
j--
for i < j && arr[i] == arr[i-1] {
i++
}
for i < j && arr[j] == arr[j+1] {
j--
}
} else if current > target {
j--
} else {
i++
}
}

return quadruplets
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package Challenge1_Quadruple_Sum_to_Target

import (
"reflect"
"testing"
)

func Test_searchQuadruplets(t *testing.T) {
type args struct {
arr []int
target int
}
tests := []struct {
name string
args args
want [][4]int
}{
{"case1", args{[]int{4, 1, 2, -1, 1, -3}, 1}, [][4]int{{-3, -1, 1, 4}, {-3, 1, 1, 2}}},
{"case2", args{[]int{2, 0, -1, 1, -2, 2}, 2}, [][4]int{{-2, 0, 2, 2}, {-1, 0, 1, 2}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := searchQuadruplets(tt.args.arr, tt.args.target); !reflect.DeepEqual(got, tt.want) {
t.Errorf("searchQuadruplets() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit bcdf30e

Please sign in to comment.