Problem · Array

Partition Into K-Size Consecutive Groups

Learn this problem
MediumDream11 logoDream11FULLTIMEONSITE INTERVIEW

Problem statement

Given an integer array nums and a positive integer k, determine whether every array element can be used exactly once to form groups of exactly k values.

Within each group, the values can be reordered as v, v + 1, ..., v + k - 1. Duplicate values are separate multiset elements. Return true if such a partition exists; otherwise return false.

Function

canPartitionConsecutive(nums: int[], k: int) → boolean

Examples

Example 1

nums = [1,2,3,6,2,3,4,7,8]k = 3return = true

The groups can be [1,2,3], [2,3,4], and [6,7,8].

Example 2

nums = [1,2,3,4,5]k = 4return = false

Five elements cannot be divided into groups of exactly four.

Constraints

  • 1 <= nums.length <= 10^5.
  • 1 <= k <= nums.length.
  • -10^9 <= nums[i] <= 10^9.

More Dream11 problems

drafts saved locally
public boolean canPartitionConsecutive(int[] nums, int k) {
    // Return whether the full multiset can be partitioned.
}
nums[1,2,3,6,2,3,4,7,8]
k3
expectedtrue
checking account