-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Challenge1_Quadruple_Sum_to_Target solution.go
- Loading branch information
1 parent
e566694
commit bcdf30e
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
Pattern2 - Two Pointers/Challenge1-Quadruple_Sum_to_Target/solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
28 changes: 28 additions & 0 deletions
28
Pattern2 - Two Pointers/Challenge1-Quadruple_Sum_to_Target/solution_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |