i-th flag is stored in flags[i]: 0 is red, 1 is white, 2 is blue.
Rearrange the flags so that all the red ones come first, then all the white ones, then all the blue ones. Return the rearranged flags. The built-in sort is off limits.
Try to do it in a single pass with O(1) extra memory.flags=[2, 1, 0, 1, 2, 0] → [0, 0, 1, 1, 2, 2]
The answer is [0, 0, 1, 1, 2, 2].
flags=[1, 0] → [0, 1]
The red flag moves in front of the white one: [0, 1].
flags=[2] → [2]
flags[i] is 0, 1 or 2flags=[2, 1, 0, 1, 2, 0]
[0, 0, 1, 1, 2, 2]
Run checks the first 3 tests in your browser. Submit checks all 38 tests on the server, hidden ones included.