BEGINNER'S BEZOS - LEETCODE THE BEGINNERS WAY || C++ thoroughly explained solution || O(nlog(n))
Observation\n\n* We need to find the largest perimeter == sum of the 3 sides should be maximum\n* But since this is a triangle the three chosen sides should sat
**Observation**\n```\n* We need to find the largest perimeter == sum of the 3 sides should be maximum\n* But since this is a triangle the three chosen sides should satisfy the condition:  largest side < (sum of other two sides)\n```\n\n**Algorithm**\n```\n1. Sort the array in descending order \n2.  Now we check if A[i] < A[i+1] + A[i+2] if true we return the perimeter as  A[i] < A[i+1] + A[i+2] \n3.  If no such condition is satisfied we return 0 as we did not find any such sides\n```\n\n**Algorithm Explanation**\n\n*  **Why do we return the ans once we find a true value for  A[i] < A[i+1] + A[i+2], why not check the rest of the array too ??**\n  We have already sorted the array in descending order so once this condition is satisfied we have obtained the highest possible perimeter in the array moving further (as the array is sorted in descending order the perimeter value will decrease. Hence there is no point in checking further.\n  Assume the testcase *[9,4,1,6,13]*\n  After sorting it becomes [13,9,6,4,1]\n  Our algorithm will return at 13+9+6  ,but we see 9+6+4 is also a valid but it is less than 13+9+6 Hence the first satisifed condition we encounter is the largest perimeter  \n\n*  **We are checking for the three sides contaigously (i,i+1,i+2). What if we valid ans at non consecutive indexes (e.g i,i+1 i+3) ?**\nThe ans *if present* will always exist on consecutive indexes\nAssume for above testcase we had array as [13,9,4,4,1]\nNow 13 < 9+4 is false but since the element after 4 are <=4 for no other successive elements the sum will exceed 13 as they lesser or equal to 4.\n\n**C++ Code**\n```\nint largestPerimeter(vector<int>& nums) {\n        \n        sort(nums.begin(),nums.end(),greater<int>());  //sorting in descending order\n        for(int i=0;i<nums.size()-2;i++){\n            if(nums[i]<(nums[i+1]+nums[i+2]))\n                return (nums[i]+nums[i+1]+nums[i+2]);\n        } \n        return 0;\n    }\n```\n\nIf you liked the solution ,do give a upvote. It helps :)\n\n
['C']