Skip to content

2294. Partition Array Such That Maximum Difference Is K #1826

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to partition an array into the minimum number of subsequences such that in each subsequence, the difference between the maximum and minimum elements is at most a given integer k.

Approach

  1. Sorting the Array: The first step is to sort the array in ascending order. Sorting helps in efficiently grouping elements into subsequences where the difference between the maximum and minimum elements is minimized.
  2. Greedy Partitioning: After sorting, we traverse the array from left to right. For each element, we start a new subsequence and include as many subsequent elements as possible such that the difference between the current element and the starting element of the subsequence does not exc…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Jun 19, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jun 19, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants