Prepin
Log in

G

Organize a binary array in linear time.

Approach / Explanation

This inquiry pertains to algorithms, particularly sorting algorithms. The binary array contains only 0's and 1's. Your task is to sort this array, which involves organizing its elements in a specific order. The particular requirement is to accomplish this in linear time. Time complexity refers to the computational complexity that indicates the amount of computational time an algorithm requires to execute, relative to the size of the program's input. For example, a time complexity of O(n) means that the execution time is directly proportional to the size of the input dataset. The essential points to consider in addressing this question are: Recognize the characteristics of a binary array. Discuss how you can leverage these characteristics to sort the array in linear time. If you are aware of specific methods or algorithms that can accomplish this, be sure to include them.

Suggested Answer

A method for sorting a binary array in linear time involves counting the total number of zeros and ones present in the array. Once we have these counts, we can populate the array with the appropriate number of zeros followed by ones. This technique leverages the fact that the array contains only two possible values: 0 and 1. The time complexity of this approach is O(n), which meets the linear time requirement for solving the problem. Here’s a Python implementation of this method: def sort_binary_array ( bin_array ) : zero_count = bin_array . count ( 0 ) one_count = len ( bin_array ) - zero_count bin_array [ : zero_count ] = [ 0 ] * zero_count bin_array [ zero_count : ] = [ 1 ] * one_count return bin_array This code first counts the number of zeros in the array. We then calculate the number of ones by subtracting the zero count from the total length of the array. Finally, we populate the first segment of the array with zeros and the second segment with ones.

Alternative Answer

A highly effective way to sort a binary array in linear time is by employing the two-pointer technique. In this method, one pointer begins at the start of the array, while the other starts at the end. When the left pointer finds a 1 and the right pointer finds a 0, we swap these values. This procedure continues until the left pointer is no longer less than the right pointer. This technique also has a time complexity of O(n). Below is a Python example that demonstrates the two-pointer technique: def sort_binary_array ( bin_array ) : left , right = 0 , len ( bin_array ) - 1 while left < right : if bin_array [ left ] == 1 and bin_array [ right ] == 0 : bin_array [ left ] , bin_array [ right ] = bin_array [ right ] , bin_array [ left ] left += 1 right -= 1 elif bin_array [ left ] == 0 : left += 1 elif bin_array [ right ] == 1 : right -= 1 return bin_array This technique sorts the array by shifting all 0s to the left and all 1s to the right without the need to count them explicitly. Therefore, both methods successfully achieve the sorting of the binary array in linear time.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Apple
Google

Product

AI Candidate AgentCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin | All rights reserved.