Back to problems

In-place Array Reordering (0/1/2 sort variant)

Algorithm · Amazon · Medium

You are given an integer array nums whose entries are limited to 0, 1, and 2. Rearrange it in place so identical values are grouped together, with all 0s first, followed by all 1s, then all 2s. Requirements: Perform the rearrangement in place and use just O(1) auxiliary memory. The intended running time is O(n). Input Format First line: an integer n Second line: n values from 0, 1, and 2 that make up nums Output Format Output the reordered array as one space-delimited line.…

Checking your access…