Challenge: Arrange a Binary Array

Solve the challenge of sorting a binary array.

Problem statement

Implement a function called SortBinaryArray(arr) that takes a binary array of numbers and returns a sorted array.

Input

An array having binary numbers.

Output

A sorted binary array.

Sample input

arr = {1, 0, 1, 0, 1, 1, 0, 0};

Sample output

result = {0, 0, 0, 0, 1, 1, 1, 1};

Coding exercise

First, take a close look at this problem and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the next section. Good luck!

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.