Sort an Array of 0's, 1's, and 2's Problem Statement

Given an integer array ARR of size 'N' containing only the values 0, 1, and 2, the task is to sort this array.

The goal is to achieve the sorting in a single pass through the array.

Example:

Input:
T = 2
N = 5
ARR = [0, 2, 1, 2, 0]
N = 3
ARR = [0, 1, 0]
Output:
[0, 0, 1, 2, 2]
[0, 0, 1]
Note:

The solution must involve iterating over the array just once.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= (5 * 105)
  • 0 <= ARR[i] <= 2
  • You need to sort the given array/list itself. No need to return or print anything explicitly.
Be the first one to answer
Add answer anonymously...
Capgemini Engineering Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter