2294. Partition Array Such That Maximum Difference Is K LeetCode Solution
In this guide, you will get 2294. Partition Array Such That Maximum Difference Is K LeetCode Solution with the best time and space complexity. The solution to Partition Array Such That Maximum Difference Is K problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Partition Array Such That Maximum Difference Is K solution in C++
- Partition Array Such That Maximum Difference Is K solution in Java
- Partition Array Such That Maximum Difference Is K solution in Python
- Additional Resources

Problem Statement of Partition Array Such That Maximum Difference Is K
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 <= 105
0 <= nums[i] <= 105
0 <= k <= 105
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
2294. Partition Array Such That Maximum Difference Is K LeetCode Solution in C++
class Solution {
public:
int partitionArray(vector<int>& nums, int k) {
ranges::sort(nums);
int ans = 1;
int mn = nums[0];
for (int i = 1; i < nums.size(); ++i)
if (mn + k < nums[i]) {
++ans;
mn = nums[i];
}
return ans;
}
};
/* code provided by PROGIEZ */
2294. Partition Array Such That Maximum Difference Is K LeetCode Solution in Java
class Solution {
public int partitionArray(int[] nums, int k) {
Arrays.sort(nums);
int ans = 1;
int mn = nums[0];
for (int i = 1; i < nums.length; ++i)
if (mn + k < nums[i]) {
++ans;
mn = nums[i];
}
return ans;
}
}
// code provided by PROGIEZ
2294. Partition Array Such That Maximum Difference Is K LeetCode Solution in Python
class Solution:
def partitionArray(self, nums: list[int], k: int) -> int:
nums.sort()
ans = 1
mn = nums[0]
for i in range(1, len(nums)):
if mn + k < nums[i]:
ans += 1
mn = nums[i]
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.