Watch 10 video solutions for Partition Array Such That Maximum Difference Is K, a medium level problem involving Array, Greedy, Sorting. This walkthrough by codestorywithMIK has 6,728 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums and an integer k. You may partition nums into one or more subsequences such that each element in nums appears in exactly one of the subsequences.
Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most k.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [3,6,1,2,5], k = 2 Output: 2 Explanation: We can partition nums into the two subsequences [3,1,2] and [6,5]. The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2. The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1. Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
Example 2:
Input: nums = [1,2,3], k = 1 Output: 2 Explanation: We can partition nums into the two subsequences [1,2] and [3]. The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1. The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0. Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
Example 3:
Input: nums = [2,2,4,5], k = 0 Output: 3 Explanation: We can partition nums into the three subsequences [2,2], [4], and [5]. The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0. The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0. The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0. Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1050 <= k <= 105Problem Overview: Given an integer array nums and an integer k, split the array into the minimum number of subsequences such that the difference between the maximum and minimum element in each subsequence is at most k. Order inside a subsequence does not matter. The goal is minimizing the number of groups.
Approach 1: Greedy Approach by Sorting (Time: O(n log n), Space: O(1) or O(n) depending on sort)
Sorting simplifies the constraint. Once nums is sorted, the smallest value in a group determines how far the group can extend. Start a group at the first element and keep adding numbers while current - start <= k. The moment the difference exceeds k, start a new group from the current element. This greedy rule works because using the smallest available value as the group's base maximizes how many elements can fit in that group. Each element is processed exactly once after sorting. Sorting dominates the runtime at O(n log n), while the scan itself is O(n). Space is O(1) if the sort is in-place.
This method relies heavily on ordering, which is why the sorting step is essential. The decision rule is purely greedy: extend the current partition as much as possible before creating a new one.
Approach 2: Sliding Window on Sorted Array (Time: O(n log n), Space: O(1))
This version frames the same idea using a window. First sort the array. Maintain two pointers: left marks the start of the current group and right expands the group. As long as nums[right] - nums[left] <= k, keep expanding right. When the difference exceeds k, the current window forms a valid subsequence. Increment the group count, move left to right, and begin forming the next group.
This interpretation highlights the technique used: a sliding window over a sorted structure. Both pointers only move forward, so the traversal remains linear after sorting. The advantage of this formulation is clarity when reasoning about range constraints, which appears frequently in array partitioning problems.
Recommended for interviews: The greedy sorted approach is the expected solution. Interviewers want to see the key observation: once the array is sorted, the smallest element in a group determines the maximum allowable element. Greedily expanding each group guarantees the minimum number of partitions. Explaining the naive idea of checking combinations helps demonstrate understanding, but the sorted greedy scan shows strong problem-solving instincts and knowledge of common array optimization patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy After Sorting | O(n log n) | O(1) extra | Best general solution. Simple scan after sorting guarantees minimum groups. |
| Sliding Window on Sorted Array | O(n log n) | O(1) | Useful when thinking about range constraints or window-based partitioning. |