Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Created June 18, 2020 10:59
Show Gist options
  • Save manojnaidu619/66a41c20221cb60855f08d8ebef00ed5 to your computer and use it in GitHub Desktop.
Save manojnaidu619/66a41c20221cb60855f08d8ebef00ed5 to your computer and use it in GitHub Desktop.
Pythagorean Triplet in an array
# Problem here -> https://www.geeksforgeeks.org/find-pythagorean-triplet-in-an-unsorted-array/
nums = [10, 4, 6, 12, 5]
found = False
for x in range(0, len(nums)): nums[x] *= nums[x]
nums.sort()
checker = len(nums) - 1
while (checker > 1):
ele = nums[checker]
a, b = 0, checker - 1
while a < b:
trip_sum = nums[a] + nums[b]
if trip_sum == ele:
print("Found")
found = True
break
if trip_sum < ele:
a+=1
else:
b-= 1
checker -= 1
if not found: print("Not Found!")
# Time complexity -> O(n^2)
# space complexity -> O(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment