Skip to content

2161. Partition Array According to Given Pivot #1385

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

You must be logged in to vote

We need to rearrange an array such that elements less than a given pivot appear first, followed by elements equal to the pivot, and finally elements greater than the pivot. The relative order of elements within the less than and greater than groups must be maintained.

Approach

  1. Separate Elements into Groups: We can divide the elements into three groups:

    • Elements less than the pivot.
    • Elements equal to the pivot.
    • Elements greater than the pivot.
  2. Maintain Relative Order: By iterating through the array and appending elements to their respective groups, we maintain their relative order as they appear in the original array.

  3. Concatenate Groups: Finally, concatenate the three groups (less …

Replies: 1 comment 2 replies

Comment options

mah-shamim
Mar 3, 2025
Maintainer Author

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

topugit Mar 3, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Mar 3, 2025
Maintainer Author

Answer selected by topugit
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