Check whether an array can be split into groups of k consecutive numbers

Quick Overview

Decide whether an integer array can be split into groups of k consecutive values, using every element exactly once. The problem tests counting with a hash map, a greedy argument about which value must begin each group, and a careful time bound for inputs of up to 10^5 elements.

Check whether an array can be split into groups of k consecutive numbers

Company: Coupang

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an array of integers `nums` and a positive integer `k`, check whether it is possible to divide this array into sets of `k` consecutive numbers. Return `True` if it is possible. Otherwise, return `False`. ### Function Signature ```python def is_possible_divide(nums: list[int], k: int) -> bool: ``` ### Rules - A set of `k` consecutive numbers has the form `x, x + 1, ..., x + k - 1` for some integer `x`. The order of elements in `nums` does not matter. - Every array position belongs to exactly one set, so a value that appears several times can go into several different sets. ### Constraints - `1 <= k <= len(nums) <= 10^5` - `1 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: True ``` The array can be divided into `[1,2,3,4]` and `[3,4,5,6]`, which uses all eight elements once. ```text Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: True ``` The array can be divided into `[1,2,3]`, `[2,3,4]`, `[3,4,5]` and `[9,10,11]`. ```text Input: nums = [1,2,3,5,6,7,8,9], k = 4 Output: False ``` The length 8 is a multiple of 4, but 1 is the smallest value, so its set would have to be `[1,2,3,4]`, and the array has no 4.

Overview: Decide whether an integer array can be split into groups of k consecutive values, using every element exactly once. The problem tests counting with a hash map, a greedy argument about which value must begin each group, and a careful time bound for inputs of up to 10^5 elements.

Coupang logo
Coupang
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums and a positive integer k, check whether it is possible to divide this array into sets of k consecutive numbers.

Return True if it is possible. Otherwise, return False.

Function Signature

def is_possible_divide(nums: list[int], k: int) -> bool:

Rules

  • A set of k consecutive numbers has the form x, x + 1, ..., x + k - 1 for some integer x . The order of elements in nums does not matter.
  • Every array position belongs to exactly one set, so a value that appears several times can go into several different sets.

Constraints

  • 1 <= k <= len(nums) <= 10^5
  • 1 <= nums[i] <= 10^9

Examples

Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: True

The array can be divided into [1,2,3,4] and [3,4,5,6], which uses all eight elements once.

Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
Output: True

The array can be divided into [1,2,3], [2,3,4], [3,4,5] and [9,10,11].

Input: nums = [1,2,3,5,6,7,8,9], k = 4
Output: False

The length 8 is a multiple of 4, but 1 is the smallest value, so its set would have to be [1,2,3,4], and the array has no 4.