DEV Community

VARUN
VARUN

Posted on

CA 09 - Move All Negative Elements to end


Problem:
Keep order of positives SAME
Keep order of negatives SAME
Move all negatives to the end

This means:

You need a stable partition
NOT swapping randomly like quicksort

Correct Approach (Simple & Clean)
Idea:
First collect all positive numbers
Then collect all negative numbers
Put back into array

Example

Input:
[1, -1, 3, 2, -7, -5, 11, 6]
Step:
positives → [1, 3, 2, 11, 6]
negatives → [-1, -7, -5]
Final:
[1, 3, 2, 11, 6, -1, -7, -5]

Top comments (0)