#include <leetcode/utils.h>

#include <assert.h>

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

int missingNumber(int *nums, int numsSize)
{
    qsort(nums, numsSize, sizeof(int), cmp);

    if (nums[0] != 0)
        return 0;

    for (int i = 1; i < numsSize; i++)
    {
        if (nums[i] - nums[i - 1] != 1)
            return nums[i] - 1;
    }

    return nums[numsSize - 1] + 1;
}

int main()
{
    int num[] = {3, 0, 1};

    ASSERT(missingNumber(num, sizeof(num) / sizeof(int)), 2, fail);

    int num2[] = {0, 1};
    ASSERT(missingNumber(num2, sizeof(num2) / sizeof(int)), 2, fail);

    return 0;

fail:
    return -1;
}
