Suppose we have a list of numbers; we have to check whether the largest number is bigger than the second-largest number by more than two times. As an example, if the list is like [3, 9, 6], then it will return false, as 9 is not bigger than 12 (2 times 6). When the list is [6, 3, 15], it will return true, as 15 is bigger than 12 (2 times 6).
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, nums): if len(nums) < 2: return False p_max = min(nums, nums) c_max = max(nums, nums) for i in range(2, len(nums)): if nums[i] > p_max: if nums[i] > c_max: p_max = c_max c_max = nums[i] else: p_max = nums[i] return c_max > p_max * 2 ob = Solution() nums = [3,6,15] print(ob.solve(nums))