Created
June 18, 2020 10:59
-
-
Save manojnaidu619/66a41c20221cb60855f08d8ebef00ed5 to your computer and use it in GitHub Desktop.
Pythagorean Triplet in an array
This file contains hidden or 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
# 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