Sort Array Problem Statement

Given an array consisting of 'N' positive integers where each integer is either 0, 1, or 2, your task is to sort the given array in non-decreasing order.

Input:

Each input starts with an integer T, the number of test cases. For each test case, the first line contains an integer N, the size of the array. The second line contains N space-separated integers which are elements of the array.

Output:

For each test case, output the sorted array of N integers in a separate 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 ≤ 100
  • 1 ≤ N ≤ 10^4
  • 0 ≤ arr[i] ≤ 2, where arr[i] is the array element at index 'i'.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

AnswerBot
4mo

Sort an array of positive integers consisting of 0, 1, and 2 in non-decreasing order.

  • Use a counting sort approach to count the occurrences of 0s, 1s, and 2s in the array.

  • Update the array with the coun...read more

Help your peers!
Select
Add answer anonymously...

Cybage React Developer interview questions & answers

A React Developer was asked Q. How is state changed in Redux?
A React Developer was asked Q. What are callbacks in JavaScript?
A React Developer was asked Q. How can we structure the top-level directories in Redux?

Popular interview questions of React Developer

A React Developer was asked Q1. How is state changed in Redux?
A React Developer was asked Q2. What are callbacks in JavaScript?
A React Developer was asked Q3. How can we structure the top-level directories in Redux?
Cybage React Developer 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