Skip to content

Commit

Permalink
tests(arrays): Add pythagorean triplet case
Browse files Browse the repository at this point in the history
  • Loading branch information
baquiax committed Jan 20, 2022
1 parent 25fedb4 commit 3c70713
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
59 changes: 59 additions & 0 deletions arrays/pythagorean.go
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
}
30 changes: 30 additions & 0 deletions arrays/pythagorean_test.go
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)
}
})
}
}

0 comments on commit 3c70713

Please sign in to comment.