Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.

&nbsp;
Example 1:

Input: nums = [2,1,2]
Output: 5


Example 2:

Input: nums = [1,2,1]
Output: 0


&nbsp;
Constraints:


	3 &lt;= nums.length &lt;= 104
	1 &lt;= nums[i] &lt;= 106


class Solution:
    def largestPerimeter(self, nums: List[int]) -> int:
        nums=sorted(nums,reverse=True)
        l=len(nums)
        for i in range(l-2):
            if nums[i]<nums[i+1]+nums[i+2]: #condition if triangle can be formed
                return  nums[i]+nums[i+1]+nums[i+2]
        return 0
class Solution {
    public int largestPerimeter(int[] nums) {
        Arrays.sort(nums);

        for(int i = nums.length - 3; i >= 0; i--) {
            if(nums[i] + nums[i + 1] > nums[i + 2])
                return nums[i] + nums[i + 1] + nums[i + 2];
        }
        return 0;
    }
}
class Solution {
public:
    int largestPerimeter(vector<int>& nums) {
	// sort the elements 
        sort(nums.begin(),nums.end());
		// iterate in everse order to get maximum perimeter
        for (int i=nums.size()-2; i>=1 ; i--){
		//Triangle is formed if sum of two sides is greater than third side
        if (nums[i]+nums[i-1] >nums[i+1])return (nums[i]+nums[i+1]+nums[i-1]); // return perimeter which is sum of three sides
        }
        return 0; // when no triangle possible it will come out of loop so return 0 here
        
    }
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var largestPerimeter = function(nums) {
    const sorted=nums.sort((a,b)=>a-b);
    const result=[];
    
    for(let i=0;i<sorted.length;i++){
        
        if((sorted[i]+sorted[i+1]>sorted[i+2])&&(sorted[i+2]+sorted[i+1]>sorted[i])&&(sorted[i]+sorted[i+2]>sorted[i+1])){
            
           result.push(sorted[i]+sorted[i+1]+sorted[i+2]);
        
        }
    }
    if(result.length!==0){
        
    return Math.max(...result); 
        
    }else{
        
    return 0;
        
    }
};
Largest Perimeter Triangle