Problem: Sort an Array of 0s, 1s, and 2s
Given an array/list ARR
consisting of integers where each element is either 0, 1, or 2, your task is to sort this array in increasing order.
Input:
The input starts with an integer ‘T’ representing the number of test cases.
For each test case:
The first line contains a positive integer ‘N’, denoting the number of elements in the array.
The second line contains ‘N’ space-separated integers, which are the elements of the array.
Output:
For each test case, output the sorted array of ‘N’ space-separated integers on a new line.
Example:
Input:
2
4
0 2 1 1
5
2 1 0 0 1
Output:
0 1 1 2
0 0 1 1 2
Explanation:
For the first test case, the sorted array is [0, 1, 1, 2].
For the second test case, the sorted array is [0, 0, 1, 1, 2].
Constraints:
- 1 ≤ T ≤ 100
- 1 ≤ N ≤ 5000
- 0 ≤ ARR[i] ≤ 2
- Time limit: 1 sec
Note:
You are not required to print anything; the output handling has already been managed. Simply implement the function to sort the array.

AnswerBot
4mo
Sort an array of 0s, 1s, and 2s in increasing order.
Use a three-pointer approach to sort the array in a single pass.
Initialize three pointers for 0, 1, and 2 values and iterate through the array.
Swap ...read more
Help your peers!
Add answer anonymously...
Top Software Developer Intern Interview Questions Asked at JPMorgan Chase & Co.
Q. Count Subarrays with Given XOR Problem Statement You are given an array of integ...read more
Q. Kth Largest Number Problem Statement You are given a continuous stream of number...read more
Q. Stack using Two Queues Problem Statement Develop a Stack Data Structure to store...read more
Interview Questions Asked to Software Developer Intern at Other Companies
Top Skill-Based Questions for JPMorgan Chase & Co. Software Developer Intern
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Operating Systems Interview Questions and Answers
250 Questions
System Design Interview Questions and Answers
250 Questions
C++ Interview Questions and Answers
150 Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

