Suppose we have a list of numbers called nums, we have to check whether there exist three numbers a, b, and c such that a^2 + b^2 = c^2.
So, if the input is like [10, 2, 8, 5, 6], then the output will be True, as 8^2 + 6^2 = 64+36 = 100 = 10^2.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, nums): tmp = sorted([n*n for n in nums], reverse = True) for i, n in enumerate(tmp): base = n left = i+1; right = len(tmp)-1 while left <= right: t = tmp[left]+tmp[right] if t == base: return True elif t > base: left += 1 else: right -= 1 return False ob = Solution() print(ob.solve([10, 2, 8, 5, 6]))
[10, 2, 8, 5, 6]