Dsa
Two Pointers
Easy
Rearrange Colors
You are given an array of integers representing colors, where 1 represents red, 2 represents white, and 3 represents blue. Your task is to sort the colors in the array so that all 1s come first, followed by all 2s, and finally all 3s. Do this with a single pass through the array using the two-pointer technique.
Input Format:
- The first line contains an integer n, the size of the array.
- The second line contains n space-separated integers representing the array.
Output Format:
- Print the sorted array.
Example:
Input:
5
3 2 1 3 2
Output:
1 2 2 3 3
Key concepts
two_pointerssortingarrays
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now