AlgoMaster Logo

Partition Array Such That Maximum Difference Is K

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We need to split an array into the fewest possible groups (subsequences) where, within each group, the gap between the largest and smallest element is at most k.

Although the problem talks about subsequences, the original order of elements is irrelevant: a group's max-min difference depends only on which values it contains. Grouping [3,1,2] or [1,2,3] gives the same range (3 - 1 = 2). The problem reduces to partitioning a collection of numbers into the fewest groups where each group's range is at most k.

Sorting is therefore the first step. Once sorted, elements that can share a group sit next to each other, and skipping over a sorted element to include a later one in the same group can only widen the range.

Key Constraints:

  • 1 <= nums.length <= 10^5 → With n up to 100,000, an O(n^2) approach performs around 10^10 operations and is too slow. We need O(n log n) or better. Sorting is O(n log n), and the greedy pass after it is O(n).
  • 0 <= nums[i] <= 10^5 → Values are non-negative and bounded, which would permit counting sort, but a standard comparison sort already meets the time budget.
  • 0 <= k <= 10^5 → k can be 0, meaning a group can only contain equal elements. It can also exceed the value range, meaning everything fits in one group. Both extremes are edge cases to check.

Approach 1: Brute Force (Check All Groups)

Intuition

Sort the array, then place each element into the first existing group that can take it. A group can accept a new element if the difference between that element and the group's minimum is at most k. If no existing group works, start a new one.

This first-fit check is valid because, after sorting, the element being placed is the largest seen so far. Adding it to a group can only raise the group's max, so the only comparison needed is against the group's min, which never changes after the group is created.

The cost is in the scan. In the worst case (k = 0 with all distinct elements), every element starts its own group, and each new element scans every existing group before failing. That is O(n^2).

Algorithm

  1. Sort nums in ascending order.
  2. Maintain a list of groups, where each group stores its minimum value.
  3. For each element in the sorted array:
    • Scan through existing groups to find one where element - group_min <= k.
    • If found, add the element to that group.
    • If no valid group is found, create a new group with this element as its minimum.
  4. Return the total number of groups.

Example Walkthrough

1After sorting: nums = [1, 2, 3, 5, 6]. Start with element 1.
0
1
i
1
2
2
3
3
5
4
6
1/6

Code

The scan over all groups is unnecessary. Group minimums increase in the order groups are created, so an element that fails the most recently created group fails every group. Tracking only the current group's minimum produces the same partition count in a single pass over the sorted array.

Approach 2: Greedy with Sorting (Optimal)

Intuition

After sorting, an optimal partition can always use contiguous runs of the array. If sorted values a <= b <= c have a and c in the same group but not b, then b can be moved into that group: c - a <= k implies b - a <= k, and adding b does not change the group's range because c is already the max and a the min.

With contiguity established, the algorithm is a single sweep. Start a group with the current element as its minimum, keep moving right while nums[i] - groupMin <= k, and start a new group at the first element that exceeds the range. The answer is the number of groups started.

Algorithm

  1. Sort nums in ascending order.
  2. Initialize partitions = 1 (we always need at least one group).
  3. Set groupMin = nums[0] (the first group starts with the smallest element).
  4. For each element from index 1 to n - 1:
    • If nums[i] - groupMin > k, the element cannot fit in the current group. Start a new group: increment partitions and set groupMin = nums[i].
    • Otherwise, the element joins the current group and nothing changes.
  5. Return partitions.

Example Walkthrough

1After sorting: [1, 2, 3, 5, 6]. partitions=1, groupMin=1
0
groupMin
1
1
2
2
3
3
5
4
6
1/6

Code