Sort Array
You are given an array consisting of 'N' positive integers where each integer is either 0 or 1 or 2. Your task is to sort the given array in non-decreasing order.
Note :
1. The array consists of only 3 distinct integers 0, 1, 2.
2. The array is non-empty.
Input Format :
The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains an integer N, denoting the size of the array.
The second line of each test case contains N space-separated integers, representing the elements of the array.
Output Format :
The only line of output of each test case consists of N integers, denoting the sorted order of elements in the given array.
Print the output of each test case in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 100
1 <= N <= 10^4
0 <= arr[i] <= 2
where arr[i] is the array element at index 'i'.
Time Limit: 1 sec
CodingNinjas
author
2y
1) The sort() method allows us to sort elements of an array in place. Besides returning the sorted array, the sort()
method changes the positions of the elements in the original array.
2) By default, th...read more
CodingNinjas
author
2y
Using STL
Algorithm
- The simplest possible approach will be to use the in-built sort function which is available in most of programming languages.
- So after applying the algorithm, we can simply return th...read more
CodingNinjas
author
2y
Three Pointer Approach
Algorithm
- Since sorting using the in-built library is trivial and we are also given that there are only three distinct integers in the array, we can use three-pointers to solve t...read more
Add answer anonymously...
Top Cybage React Developer interview questions & answers
Popular interview questions of React Developer
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app