bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2161. Partition Array According to Given Pivot

2161. Partition Array According to Given Pivot

Leetcode Solutions

Three-Pass Partitioning

  1. Initialize three empty lists (or arrays) for elements less than, equal to, and greater than the pivot.
  2. Iterate through the input array nums and for each element:
    • If it is less than pivot, add it to the 'less than' list.
    • If it is equal to pivot, add it to the 'equal to' list.
    • If it is greater than pivot, add it to the 'greater than' list.
  3. Concatenate the three lists in the order of 'less than', 'equal to', and 'greater than' to form the rearranged array.
  4. Return the rearranged array.
UML Thumbnail

In-Place Two-Pointer Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...