// This file is generated by mkfile.py
// Date: 2025-10-18

#include <solution/3397.h>
#include <stdlib.h>
int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

int maxDistinctElements(int *nums, int numsSize, int k)
{
    if (numsSize == 0)
        return 0;

    qsort(nums, numsSize, sizeof(int), cmp);
    int result = 1;
    int prev = nums[0] - k;

    for (int i = 1; i < numsSize; i++)
    {
        int min = nums[i] - k;
        int max = nums[i] + k;

        if (prev >= min)
        {
            if (max <= prev)
            {
                continue;
            }
            prev += 1;
        }
        else
        {
            prev = min;
        }
        result++;
    }

    return result;
}
