Python/Java/JS/Go/C++ O( n log n ) by sorting. [w/ Visualization]
O( n log n ) by sorting. \n\n---\nHint:\n\nSort input side length in descending order.\n\nTry and test the side length combination of triangle from the largest 
O( n log n ) by sorting. \n\n---\n**Hint**:\n\n**Sort** input side length in descending order.\n\n**Try and test** the side length combination of triangle **from the largest one**.\n\nIf we find the optimal one, then early return the sum of valid largest side length combination.\nOtherwise, return 0 (i.e., impossible to make triangle)\n\n---\n\n**Recall**:\n\nBasic property of triangle:\n\n**[Triangle inequality](https://en.wikipedia.org/wiki/Triangle_inequality)**\n\n![image](https://assets.leetcode.com/users/brianchiang_tw/image_1585128080.png)\n\n\nZ < X + Y where X, Y, Z are valid side length of triangle.\n\n---\nImplementation:\n```\nclass Solution:\n    def largestPerimeter(self, A: List[int]) -> int:\n        \n\t\t# sort side length in descending order\n        A.sort( reverse = True )\n        \n\t\t# Try and test from largest side length\n        for i in range( len(A) - 2):\n            \n            if A[i] < A[i+1] + A[i+2]:\n                # Early return when we find largest perimeter triangle\n                return A[i] + A[i+1] + A[i+2]\n        \n        # Reject: impossible to make triangle\n        return 0\n        \n```\n\n---\n\nJava:\n\n```\n\nclass Solution {\n    public int largestPerimeter(int[] nums) {\n        \n        \n        // Java native int[] sort doesn\'t support reverse option\n        Arrays.sort( nums );\n\n        for( int i = nums.length-3 ; i >= 0 ; i--){\n            \n            if( nums[i+2] < ( nums[i+1] + nums[i] ) ){\n                // Accept: find the triangle with largest perimeter\n                return ( nums[i] + nums[i+1] + nums[i+2] );\n            }\n            \n        }\n        \n        // Reject: impossible to make triangle\n        return 0;\n    }\n}\n```\n\n---\n\nJavascript:\n\n```\nvar compareFn = function(a, b){\n    // put larger element of the left hand side\n    return b - a;    \n}\n\nvar largestPerimeter = function(nums) {\n    \n    nums.sort( compareFn );\n    \n    for( let i = 0 ; i < nums.length-2 ; i++){\n        \n        if( nums[i] < ( nums[i+1] + nums[i+2]) ){\n            \n            // Accept: find the triangle with largest perimeter\n            return ( nums[i] +  nums[i+1] + nums[i+2] )\n        }\n        \n    }\n    \n    // Reject: impossible to make triangle\n    return 0;\n};\n```\n\n---\n\nGo:\n\n```\nimport(\n    "sort"\n)\n\nfunc largestPerimeter(nums []int) int {\n    \n    // sort side length in descending order\n    sort.Slice( nums, func( i, j int) bool{\n        return nums[i] > nums[j]\n    })\n    \n    \n    for i := 0 ; i < len(nums)-2 ; i++ {\n\n        // Accept: find the triangle with largest perimeter\n        if( nums[i] < ( nums[i+1] + nums[i+2]) ){\n            return nums[i] + nums[i+1] + nums[i+2]\n        }\n\n    }\n    \n    // Reject: impossible to make triangle\n    return 0\n}\n```\n\n---\n\nC++\n\n```\nclass Solution {\npublic:\n    int largestPerimeter(vector<int>& nums) {\n        \n        // sort side length in descending order\n        std::sort( nums.rbegin(), nums.rend() );\n        \n        for( int i = 0 ; i < nums.size()-2 ; i++ ){\n            \n            // Accept: find the triangle with largest perimeter\n            if( nums[i] < ( nums[i+1] + nums[i+2]) ){\n                return nums[i] + nums[i+1] + nums[i+2];\n            }\n            \n        }\n        \n        // Reject: impossible to make triangle\n        return 0;\n    }\n};\n```\n\n---\n\nRelated leetcode challenge:\n\n[Leetcode #611 Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number/)\n\n[Leetcode #812 Largest Triangle Area](https://leetcode.com/problems/largest-triangle-area/)\n\n---\n\nReference:\n\n[1] [Python official docs about sort( ) with direction](https://docs.python.org/3/howto/sorting.html#ascending-and-descending)\n\n[2] [Wiki: Triangle inequality](https://en.wikipedia.org/wiki/Triangle_inequality)\n\n[3] [MDN docs: Javascript Array.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)\n\n[4] [CPP plus docs: STL sort()](https://cplusplus.com/reference/algorithm/sort/)\n\n[5] [Go offical docs: sort](https://pkg.go.dev/sort)\n\n[6] [Java official docs: sort](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[],%20int,%20int))
['Math', 'C', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript']