imashutosh51

Largest Perimeter Traingle

Feb 3rd, 2026
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. '''
  2. Condition of traingle with sides a,b and c -> a<=b<=c and a+b>c
  3. we want the highest perimeter also so we will sort the order now so we will meet one condition a<=b<=c now you have to check a+b>c or not so take the right most element and check the previous two element's sum are greater than that elment or not?
  4.  
  5. if not then move all a,b,c one side left as if previous c you couldn't beat by sum of prev a and b then after reducing a & b,the new a & b are more smaller so better reduce c also for checking all the possible sides.
  6. '''
  7.  
  8. class Solution:
  9.     def largestPerimeter(self, nums: List[int]) -> int:
  10.         nums.sort()
  11.         i = len(nums) - 1
  12.  
  13.         while i >= 2:
  14.             if nums[i-2] + nums[i-1] > nums[i]:
  15.                 return nums[i-2] + nums[i-1] + nums[i]
  16.             i -= 1
  17.  
  18.         return 0
  19.  
Advertisement
Add Comment
Please, Sign In to add comment