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:
N = 5
A[] = {1,0,1,1,0}
Output: 0 0 1 1 1

Example 2:
Input:
N = 10
A[] = {1,0,1,1,1,1,1,0,0,0}
Output: 0 0 0 0 1 1 1 1 1 1
Your Task:
Complete the function SortBinaryArray() which takes given array as input and returns the sorted array. 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Challenge: Try doing it in one pass.
Constraints:
1 <= N <= 106
0 <= A[i] <= 1
0
vishalja77194 days ago
 vector<int> SortBinaryArray(vector<int> binArray)
    {
        // Your code goes here 
        int zero=0,ones=0;
        for(int i=0;i<binArray.size();i++)
        {
            if(binArray[i] == 0)
                zero++;
            if(binArray[i] == 1)
                ones++;
        }
        int k=0;
        for(int i=0;i<zero;i++)
        {
            binArray[k++]=0;
        }
        for(int i=0;i<ones;i++)
        {
            binArray[k++]=1;
        }
        return binArray;
    }
+1
harshscode6 days ago
in O(N) and O(1)
        int z=0,o=0;       for(int i=0;i<b.size();i++)       {           if(b[i]==0)           z++;                      if(b[i]==1)           o++;                  }       int k=0;       for(int i=0;i<z;i++)       b[k++]=0;              for(int i=0;i<o;i++)       b[k++]=1;              return b;
0
mestryruturaj1 week ago
Java: O(N)
class Solution 
{ 
	static int[] SortBinaryArray(int arr[], int n) 
	{ 
	    int left = 0;
	    for (int i = 0; i < n; i++) {
	        if (arr[i] == 0) {
	            arr[i] = arr[left];
	            arr[left++] = 0;
	        }
	    }
	    return arr;
	} 
} 

0
kssaini32011 week ago
C++
 
class Solution{   public:   vector<int> SortBinaryArray(vector<int> binArray)   {       // Your code goes here        sort(binArray.begin(),binArray.end());       return binArray;   }};
+1
zerefkhan2 weeks ago
C++ solution
Total Time Taken:
0.98/2.36
    vector<int> SortBinaryArray(vector<int> binArray)
    {
        int left = 0, right = (binArray.size())-1;
        while(left < right){
            if (binArray[left] == 0) left++;
            else if (binArray[right] == 1) right--;
            else {
                // Swapping elements at the position left and right
                int swap_int = binArray[left];
                binArray[left] = binArray[right];
                binArray[right] = swap_int;
                
                left++;
                right--;
            }
        }
        return binArray;
    }
+1
bipulharsh1232 weeks ago
vector<int> SortBinaryArray(vector<int> binArray)   {       // Your code goes here        int oneInd = -1;       for(int ind=0; ind<binArray.size(); ind++)           if(binArray[ind]==1){                   oneInd = ind;                   break;           }       if(oneInd==-1)           return binArray;       for(int ind=oneInd+1; ind<binArray.size(); ind++)           if(binArray[ind]==0){               swap(binArray[ind], binArray[oneInd]);               oneInd++;           }       return binArray;   }
 
Space: O(1)
Time: O(n)
0
mayank180919992 weeks ago
    vector<int> SortBinaryArray(vector<int> binArray)
    {
        // Your code goes here 
        vector<int>ans;
        for(int i=0;i<binArray.size();i++){
            if(binArray[i]==0){
                ans.push_back(binArray[i]);
            }
        }
        for(int i=0;i<binArray.size();i++){
            if(binArray[i]!=0){
                ans.push_back(binArray[i]);
            }
        }
    return ans;
    }
0
dsm2 weeks ago
These 2 questions are same. Just differ in use of array and vector. 
Same solution required and same statement.
0
__cloude__2 weeks ago
vector<int>res;       int z=0,o=0;       for(auto i:binArray){           if(i==0)++z;           else ++o;       }       for(int i=1;i<=z;++i){           res.push_back(0);       }       for(int i=1;i<=o;++i){           res.push_back(1);       }       return res;
+2
shreyashobha02013 weeks ago
//simple and clean cpp solution
vector<int> SortBinaryArray(vector<int> b)   {       int i = 0, j = 0;       while(i < b.size())       {           if(b[i] == 0)               swap(b[i], b[j++]);           i++;       }       return b;   }
 
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.