Skip to content

Instantly share code, notes, and snippets.

@heymonicakay
Last active October 21, 2020 01:56
Show Gist options
  • Save heymonicakay/0936dc9c5a3c59f0afbcbcb0e3eb8a7f to your computer and use it in GitHub Desktop.
Save heymonicakay/0936dc9c5a3c59f0afbcbcb0e3eb8a7f to your computer and use it in GitHub Desktop.
Pythagorean Triplet Check

Given an array of integers, determine whether it contains a Pythagorean triplet. Recall that a Pythogorean triplet (a, b, c) is defined by the equation a2+ b2= c2.

Example 1:
Input: [2, 5, 6, 7, -4, 3]
Output: true
Explanation: 3 squared + -4 squared = 5 squared

Example 2:
Input: [25, -4, 10, 7]
Output: false
Explanation: no Pythagorean triplet
1. iterate over an array of integers
2. verify that they are integers
if they are integers then
simplified
arr [3, 4, 5, 2]
assuming all values are integers
find all possible sums of two numbers in array
push sums into new sumArray [7, 8, 5, 9, 6, 7]
square all values store in squaredArray
first iteration
3 + 4 = 7
3+ 5 = 8
3 + 2 = 5
4 + 5 = 9
4 + 2= 6
5 +2 =7
1 -> originalArray < iterating over this array to sum the values and store sums in new array sumArray
2 -> sumArray < iterate over this array > iterate back over the originalArray
const originalArray = [3, 4, 5, 2]
const pythagTripCheck = (originalArray) => {
let squaredArray = [];
let sumArray = [];
for(i = 0; i < originalArray.length; i++){
let squared = Math.pow(originalArray[i], 2)
squaredArray.push(squared)
}
let sum = originalArray[i] + originalArray[i+1];
sumArray.push(sum)
console.log(sumArray, "SUM ARRAY")
return sumArray
}
console.log(pythagTripCheck(originalArray))
// for(j = originArray[i+1], j < originalArray.length, j++) {
// //add value of num to the originalArray[j]
// // push the value of the sum into sumArray
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment