Beats 92.8% 🔥|| Python3 🐍 || Easy 🚀 
IntuitionA triangle is valid if the sum of any two sides is greater than the third side. To maximize the perimeter, we should try using the largest possible sid
![Todayyyyy.png](https://assets.leetcode.com/users/images/4754070d-a9e3-4aa0-99be-a7c9ef8b459d_1739088631.7729247.png)


# Intuition
A triangle is valid if the sum of any two sides is greater than the third side. To maximize the perimeter, we should try using the largest possible sides first. Sorting the array helps us efficiently check for the largest valid triangle.

# Approach
1. **Sort the array in descending order** – This ensures we check the largest possible sides first, increasing the chances of getting the maximum perimeter.
2. **Iterate through the array** – Start from the largest three numbers and move down, checking if they satisfy the triangle condition:  
   nums[i] < nums[i+1] + nums[i+2]
3. **Return the first valid perimeter** – Since we are checking from the largest values, the first valid set gives the maximum perimeter.
4. **Return 0 if no valid triangle is found** – If no triplet satisfies the condition, return 0.

# Complexity
- **Time complexity:** $$O(n \log n)$$ due to sorting.
- **Space complexity:** $$O(1)$$ since we only use a few extra variables.


# Code
```python3 []
class Solution:
    def largestPerimeter(self, nums: List[int]) -> int:
        nums.sort(reverse=True)
        n = len(nums)

        i = 0

        while i < len(nums) - 2:
            if nums[i] < nums[i+1] + nums[i+2]:
                return nums[i] + nums[i+1] + nums[i+2]
            i += 1
        return 0
```
['Python3']