-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(arrays): Add pythagorean triplet case
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
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,59 @@ | ||
package arrays | ||
|
||
import ( | ||
"math" | ||
"sort" | ||
) | ||
|
||
// Given an array of integers, write a function that returns true if | ||
// there is a triplet (a, b, c) that satisfies a^2 + b^2 = c^2. => a^2+b^2-c^2=0 | ||
|
||
// Example: | ||
|
||
// Input: arr[] = {3, 1, 4, 6, 5} | ||
// Output: True | ||
// There is a Pythagorean triplet (3, 4, 5). | ||
|
||
// Input: arr[] = {10, 4, 6, 12, 5} | ||
// Output: False | ||
// There is no Pythagorean triplet. | ||
|
||
// 1. Looking the first triplet (one is enough) | ||
// Naive approach is doing a triple nested for to build all the triplets possible, | ||
// checking if the three elements in every iteration fulfill the criteria. O(n^3) | ||
// | ||
// 2. I can order the array. O(n log n) | ||
// 2.1 Iterate over the ordered array O(n) | ||
// 3.2 Iterate in the array with two indexes trying to find those two numbers that gives as | ||
// a result the element in the first loop O(n) | ||
// O(n log n) + O (n) * O(n) => O(n lon g) + O(n^2) | ||
|
||
// NOTE: A B and C should be on that order in the array. | ||
func hasPythagoreanTriplet(array []int) bool { | ||
sort.Ints(array) // n log n | ||
|
||
// a^2 = c^2 - b^2 | ||
for c := len(array) - 1; c >= 0; c-- { // n | ||
a := 0 | ||
b := len(array) - 1 | ||
cSquare := math.Pow(float64(array[c]), 2) | ||
for a < b { | ||
aSquare := math.Pow(float64(array[a]), 2) | ||
bSquare := math.Pow(float64(array[b]), 2) | ||
currentC := aSquare + bSquare | ||
if currentC < cSquare { | ||
a++ | ||
continue | ||
} | ||
|
||
if currentC > cSquare { | ||
b-- | ||
continue | ||
} | ||
|
||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
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,30 @@ | ||
package arrays | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPythagoreanTriplet(t *testing.T) { | ||
tests := map[string]struct { | ||
given []int | ||
want bool | ||
}{ | ||
"test 1": { | ||
given: []int{3, 1, 4, 6, 5}, | ||
want: true, | ||
}, | ||
"test 2": { | ||
given: []int{10, 4, 6, 12, 5}, | ||
want: false, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
result := hasPythagoreanTriplet(test.given) | ||
if test.want != result { | ||
t.Fatalf("wanted %v but got %v", test.want, result) | ||
} | ||
}) | ||
} | ||
} |