Binary Array Sorting Problem Statement
You are provided with a binary array, i.e., an array containing only 0s and 1s. Your task is to sort this binary array and return it after sorting.
Input:
The first line contains an integer ‘T’ denoting the number of test cases.
The following ‘2*T’ lines correspond to the test cases.
The first line of each test case contains an integer ‘N’ representing the size of the array.
The second line of each test case contains ‘N’ space-separated integers comprising 0s and 1s, which are the elements of the array.
Output:
For each test case, output ‘N’ space-separated integers that represent the elements of the sorted binary array on a separate line.
Each test case should be printed on a new line.
Example:
Input:
2
5
1 0 1 0 1
4
0 0 1 1
Output:
0 0 1 1 1
0 0 1 1
Constraints:
1 <= T <= 50
1 <= N <= 104
0 <= arr[i] <= 1
Note
You do not need to print anything; the function should solely implement the logic for sorting.
Follow Up
1. Can you achieve the solution in linear time and constant space?
2. Is it possible to accomplish this in a single traversal?

AnswerBot
4mo
Yes, the binary array can be sorted in linear time and constant space using a single traversal.
Use two pointers approach to swap 0s to the left and 1s to the right.
Maintain two pointers, one for 0s an...read more
Help your peers!
Add answer anonymously...
Times Internet Software Developer interview questions & answers
A Software Developer was asked Q. Write a function to delete a node in a singly linked list, given only access to ...read more
A Software Developer was asked Q. Which data structure would you use to program a jigsaw puzzle program, and what ...read more
A Software Developer was asked Q. What is polymorphism? Can you provide some examples?
Popular interview questions of Software Developer
A Software Developer was asked Q1. Write a function to delete a node in a singly linked list, given only access to ...read more
A Software Developer was asked Q2. Which data structure would you use to program a jigsaw puzzle program, and what ...read more
A Software Developer was asked Q3. What is polymorphism? Can you provide some examples?
>
Times Internet Software Developer Interview 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

