Sort 0 1 2 Problem Statement

Given an integer array arr of size 'N' containing only 0s, 1s, and 2s, write an algorithm to sort the array.

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case consists of two lines. The first line contains an integer 'N', the size of the array. The second line contains 'N' space-separated integers, the elements of the array.

Output:

For each test case, output the sorted array as space-separated integers in a new line.

Example:

Input:
2
5
0 2 1 2 0
3
2 1 0
Output:
0 0 1 2 2
0 1 2

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 5 * 105
  • 0 ≤ arr[i] ≤ 2
  • Time Limit: 1 sec

Note:

Try to solve the problem using a single scan over the array. You need to modify the array in-place and do not return anything.

AnswerBot
4mo

Sort an integer array containing only 0s, 1s, and 2s in-place using a single scan.

  • Iterate through the array using three pointers to keep track of 0s, 1s, and 2s.

  • Swap elements based on the current elem...read more

Shalini Saini
1y

//User function Template for C

Void sort012(int a[], int n)

{

}

Help your peers!
Select
Add answer anonymously...

Deloitte Business Technology Analyst interview questions & answers

A Business Technology Analyst was asked 9mo agoQ. How do you clone a database?
A Business Technology Analyst was asked 9mo agoQ. How do you find and resolve block level corruption?
A Business Technology Analyst was asked 9mo agoQ. How do you use LogMiner?

Popular interview questions of Business Technology Analyst

A Business Technology Analyst was asked 9mo agoQ1. How do you clone a database?
A Business Technology Analyst was asked 9mo agoQ2. How do you find and resolve block level corruption?
A Business Technology Analyst was asked 9mo agoQ3. How do you use LogMiner?

Top HR questions asked in Deloitte Business Technology Analyst

A Business Technology Analyst was asked 9mo agoQ1. How did you come to know about Deloitte?
A Business Technology Analyst was asked Q2. Explain your experience and projects.
A Business Technology Analyst was asked Q3. What did you learn in your engineering course?
Deloitte Business Technology Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits