#include <solution/976.h>
#include <stdlib.h>

int cmp(const void *a, const void *b)
{
    return *(const int *)b - *(const int *)a;
}

int largestPerimeter(int *nums, int numsSize)
{
    qsort(nums, numsSize, sizeof(int), cmp);
    int i = 0, j = 1;

    while (i < numsSize - 2 && j < numsSize - 1)
    {
        int k = j + 1;
        if (nums[i] < nums[j] + nums[k])
            return nums[i] + nums[j] + nums[k];
        else
        {
            j++;
            i++;
        }
    }

    return 0;
}
