🔥 Largest Perimeter Triangle 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
\uD83D\uDD25 Largest Perimeter Triangle \uD83D\uDD25 || 5 Solutions || Simple Fast and Easy || with Explanation\n\n## Solution - 1\n\n### Explanation\n- Before 
# \uD83D\uDD25 Largest Perimeter Triangle \uD83D\uDD25 || 5 Solutions || Simple Fast and Easy || with Explanation\n\n## Solution - 1\n\n### Explanation\n- Before diving into code.. Let us recall the basic rule for lengths of sides of a triangle\n i.e, sum of any two sides of a triangle should be greater than its third side.\n Simply, a + b > c\n-  where a, b, c are sides of triangle\n ### Approach:\n Since we need the largest perimeter from the given nums. We sort the array and traverse from the end\n\n-  if nums[i] < nums[i-1] + nums[i-2], then\n we Stop and return the perimeter==> nums[i] + nums[i-1] + nums[i-2]\n\n-  if there is no values that satisfies the given condition then\n we return 0\n\n```dart\nclass Solution {\n// Runtime: 426 ms, faster than 80.00% of Dart online submissions for Largest Perimeter Triangle.\n// Memory Usage: 149.6 MB, less than 100.00% of Dart online submissions for Largest Perimeter Triangle.\n\n  int largestPerimeter(List<int> nums) {\n    nums.sort();\n    for (int i = nums.length - 1; i > 1; --i)\n      if (nums[i] < nums[i - 1] + nums[i - 2])\n        return nums[i] + nums[i - 1] + nums[i - 2];\n    return 0;\n  }\n}\n```\n\n## Solution - 2 Recursive\n\n```dart\nclass Solution {\n// Runtime: 514 ms, faster than 60.00% of Dart online submissions for Largest Perimeter Triangle.\n// Memory Usage: 150.2 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle.\n\n  //utility method to get max element at given index\n  void swapToGetMax(List<int> nums, int index) {\n    int max = 0, maxIndex = -1;\n    for (int i = 0; i <= index; i++)\n      if (max < nums[i]) {\n        max = nums[i];\n        maxIndex = i;\n      }\n    //actual swapping after finding max element in given range\n    int temp = nums[index];\n    nums[index] = max;\n    nums[maxIndex] = temp;\n  }\n\n  int largestPerimeter(List<int> nums) {\n    //if array has only 3 elements\n    if (nums.length == 3) {\n      if (nums[0] < nums[1] + nums[2] &&\n          nums[1] < nums[0] + nums[2] &&\n          nums[2] < nums[1] + nums[0])\n        return nums[0] + nums[1] + nums[2];\n      else\n        return 0;\n    }\n    //for more than 3 elements, without doing explicit sorting\n    int n = nums.length;\n    //here we are putting max element at last index\n    swapToGetMax(nums, n - 1);\n    //here we are putting max element at second last index\n    swapToGetMax(nums, n - 2);\n    //here we are putting max element at third last index\n    swapToGetMax(nums, n - 3);\n    //in loop checking if nums[i]<nums[i-1]+nums[i-2] which this triplet will form the max perimeter\n    for (int i = n - 1; i >= 2; i--) {\n      if (nums[i] < nums[i - 1] + nums[i - 2])\n        return nums[i] + nums[i - 1] + nums[i - 2];\n      //if not then will find max element as (i-3)th largest element\n      else if (i > 2) swapToGetMax(nums, i - 3);\n    }\n    return 0;\n  }\n}\n```\n\n## Solution - 3\n\n```dart\nclass Solution {\n// Runtime: 596 ms, faster than 40.00% of Dart online submissions for Largest Perimeter Triangle.\n// Memory Usage: 150.3 MB, less than 20.00% of Dart online submissions for Largest Perimeter Triangle.\n  int largestPerimeter(List<int> nums) {\n    if (nums.length == 3) {\n      if (nums[0] < nums[1] + nums[2] &&\n          nums[1] < nums[0] + nums[2] &&\n          nums[2] < nums[1] + nums[0])\n        return nums[0] + nums[1] + nums[2];\n      else\n        return 0;\n    }\n    int n = nums.length;\n    int maxi = 0;\n    for (int i = 0; i < n - 2; i++) {\n      for (int j = i + 1; j < n - 1; j++) {\n        for (int k = j + 1; k < n; k++) {\n          int a = nums[i];\n          int b = nums[j];\n          int c = nums[k];\n          if (a < b + c && b < c + a && c < a + b) maxi = max(maxi, a + b + c);\n        }\n      }\n    }\n    if (maxi > 0) return maxi;\n    return 0;\n  }\n}\n```\n\n## Solution - 4\n\n```dart\nclass Solution {\n  int largestPerimeter(List<int> nums) {\n    //sort the vector\n    nums.sort();\n    //any triangle sum of smaller two side greater than 3rd side...so we check that condition, a+b>c where a<b<c\n    int maxPerimeter = 0;\n\n    for (int i = 0; i <= nums.length - 3; i++) {\n      //check the triangle valid condition\n      if (nums[i] + nums[i + 1] > nums[i + 2]) {\n        maxPerimeter =\n            max(maxPerimeter, nums[i] + nums[i + 1] + nums[i + 2]); //find max\n      }\n    }\n\n    return maxPerimeter; //return the result\n  }\n}\n```\n\n## Solution - 5\n\n```dart\nclass Solution {\n  int largestPerimeter(List<int> nums) {\n    nums.sort((a, b) => a - b);\n    int i = nums.length - 1;\n    while (i >= 0) {\n      if (nums[i] < nums[i - 1] + nums[i - 2]) {\n        return nums[i] + nums[i - 1] + nums[i - 2];\n      } else {\n        i--;\n      }\n    }\n    return 0;\n  }\n}\n```\n## [GitHub Link](https://github.com/ayoubzulfiqar/leetcode)
['Recursion', 'Sorting']