Binary Array Sorting | Practice | GeeksforGeeks

Given a binary array A[] of size N. The task is to arrange the array in increasing order.
Note: The binary array contains only 0  and 1.
 
Example 1:
Input: 
5
1 0 1 1 0

Output: 
0 0 1 1 1

Explanation: 
After arranging the elements in 
increasing order, elements will be as 
0 0 1 1 1.
Example 2:
Input:
10
1 0 1 1 1 1 1 0 0 0

Output: 
0 0 0 0 1 1 1 1 1 1

Explanation: 
After arranging the elements in 
increasing order, elements will be 
0 0 0 0 1 1 1 1 1 1.


Your Task: This is a function problem. You only need to complete the function binSort() that takes the array A[] and it's size N as parameters and sorts the array. The printing is done automatically by the driver code.

Expected Time Complexity: O(N)
Expected Auxilliary Space: O(1)

Constraints:
1 <= N <= 106
0 <= A[i] <= 1
0
mehtay037in 6 hours
Just like the question segregate 0s and 1s
Python Solution:
 
class Solution:      #Function to sort the binary array.   def binSort(self, A, N):        #Your code here       #No need to print the array       index=0       for i in range(N):           if(A[i]==0):               A[index],A[i]=A[i],A[index]               index+=1       return A 
 
0
mitrasouvik1235 hours ago
class Solution{   //Function to sort the binary array.   static void binSort(int A[], int N)   {       int left = 0; int right = N-1;              while(left<right)       {           while(A[left]==0 && left<right)           {               left++;           }           while(A[right]==1 && left<right)           {               right--;           }           if(left<right)           {               int temp = A[right];               A[right] = A[left];               A[left] = temp;               left++;               right--;           }       }   }   } 
0
jainkunal20037 hours ago
int j=0;
        
        for(int i=0;i<N;i++){
            if(A[i]!=1){
                swap(A[i],A[j]);
                j++;
                
            }
        }
0
jainkunal2003
This comment was deleted.
+1
harshscode6 days ago
most efficient inn  O(n) and O(1)...
int z=0,o=0;       for(int i=0;i<n;i++)       {           if(a[i]==0)           z++;           else if(a[i]==1)           o++;          }       int j=0;       for(int i=0;i<z;i++)       a[j++]=0;       for(int i=0;i<o;i++)       a[j++]=1;
0
ojaskulkarni141 week ago
SIMPLE JAVA SOLUTION 
static void binSort(int A[], int N)
    {
        int sum=0;
        for(int i=0;i<N;i++)sum+=A[i];
        
        int c= N-sum;
        
        for(int i=0;i<N;i++){
            if(c>0){
                A[i]=0;
                c--;
            }
            else A[i]=1;
        }
    }
0
sumitk071 week ago
sort(A,A+N);
0
mestryruturaj1 week ago
Java: O(N)
class Solution
{
    //Function to sort the binary array.
    static void binSort(int A[], int N)
    {
        if (N == 1) return;
        
        for (int i = 0, left = 0; i < N; i++) {
            if (A[i] == 0) {
                int temp = A[left];
                A[left] = 0;
                A[i] = temp;
                left++;
                
            }
        }
        
    }
}
0
madhukartemba2 weeks ago
JAVA SOLUTION:
class Solution
{
    //Function to sort the binary array.
    static void binSort(int A[], int N)
    {
        // add your code here
        int low = 0;
        int high = N-1;
        
        while(low<high)
        {
            if(A[low]==1)
            {
                A[low] = A[high];
                A[high--] = 1;
            }
            else
            {
                low++;
            }
        }
        
        /**************
        * No need to print the array
        * ************/
    }
}
0
shubham211019972 weeks ago
simplest solution
 
void binSort(int arr[], int n)   {      int i=-1;      for(int j=0;j<n;j++){      if(arr[j]==0){          i++;          swap(arr[i],arr[j]);      }    }   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.