Binary Array Sorting
A binary array is an array consisting of only 0s and 1s.
You are given a binary array "arr" of size ‘N’. Your task is to sort the given array and return this array after sorting.
Input Format :
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ 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 consisting of 0s and 1s as the array elements.
Output Format :
For each test case, print ‘N’ space-separated integers representing the elements of the sorted binary array in a separate line.
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.
Follow Up :
1. Can you solve this problem in a linear time and constant space?
2. Can you solve this problem in a single traversal only?
Constraints :
1 <= T <= 50
1 <= N <= 10^4
0 <= arr[i] <= 1
Where ‘T’ is the number of test cases and ‘N’ is the size of the array.
Time Limit: 1 sec
CodingNinjas
author
2y
A two-pointer approach can be used for this question. Maintain two indexes. Initialize the first index left as 0 and second index right as n-1, where n is size of the array .
While left < right , do th...read more
CodingNinjas
author
2y
Counting
- Initialize an integer variable ‘countZero’:= 0, It will store the count of zeros in the given array.
- Iterate over the given array, if the current element is 0 then increment ‘countZero’ by 1.
- R...read more
CodingNinjas
author
2y
One pass of quick Sort
This problem can be solved in a single traversal also. The concept used for this approach is related to the partition of quicksort. In a quick sort’ partition, after one scan, th...read more
Add answer anonymously...
Top Times Internet Senior Android Developer interview questions & answers
Popular interview questions of Senior Android Developer
>
Times Internet Senior Android Developer Interview Questions
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