singularity

1296. Divide Array in Sets of K Consecutive Numbers

O(n log n)

The code first sorts the input array which takes O(n log n) time complexity. Then it iterates through the groups of size k, which would take O(n) time. Therefore, the overall time complexity is O(n log n).

see also


var isPossibleDivide = function (nums, k) {
    if (nums.length % k) return false;

    let check = true;

    let gp = Array.from({
        length: nums.length / k
    }, () => []);

    let s = nums.sort((a, b) => a - b);

    for (g of gp) {
        while (g.length < k) {
            if (!g.length) {
                g.push(s[0]);
                s.splice(0, 1);
            } else {
                let val = s.find(x => x > g[g.length - 1]);
                g.push(val);
                s.splice(s.indexOf(val), 1);
            }
        }

        if (g.length != k) {
            check = false;
        }

        for (let i = 1; i < g.length; i++) {
            if (g[i] != g[i - 1] + 1) {
                check = false;
            }
        }
    }
    return check;
};