Signup/Sign In

Program to separate 0s on the left side and 1s on the right side

In this tutorial, we will learn how to separate 0s on the Left side and 1s on the right side of the array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: 0 0 1 1 0 1 0 1 1 0 1 1 1

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

Program 1: Separate 0s on the left side and 1s on the right side

In this method, we will see how to separate 0s on the left side and 1s on the right side of the array using the sorting technique.

Algorithm

  1. Start
  2. Declare the array size.
  3. Ask the user to initialize the array size.
  4. Declare the array.
  5. Ask the user to initialize the array elements.
  6. Check whether the entered elements are 0 and 1 or not.
  7. If entered elements are other than 0 and 1 ask the user to enter again.
  8. If entered elements are 0 and 1 then sort the array using arrays.sort()
  9. This sorting will keep the 0s in the left side and 1s on the right side.
  10. Print the sorted array.
  11. Stop.

The below program demonstrates how to separate 0s on the Left side and 1s on the right side of the array using the sorting technique.

import java.util.*;  
import java.util.Arrays; 
//Driver Code
public class Main  
{  
    static void printElements(int arr[],int n)
    {
        System.out.println("Resultant Array is ");
        for(int i=0;i<n;i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println(" ");
    }
   public static void main(String args[])   
   {  
       Scanner sc=new Scanner(System.in);
      int n;    //Declare array size
      System.out.println("Enter the total number of elements ");
      n=sc.nextInt();     //Initialize array size
      int arr[]=new int[n];   //Declare array
      System.out.println("Enter the elements of the array ");
      for(int i=0; i<n ;i++)     //Initialize array
      {
          arr[i]=sc.nextInt();
      }      
      int flag=1;      
        for(int t=0;t<n;t++)
        {
            if(arr[t]==0 || arr[t]==1)
            {
               Arrays.sort(arr);     //Sort the array
               flag++;
           }
          else
          {
                 flag=0;
          }
      }      
      if(flag==0)
      {
          System.out.println("Elements other than 0 and 1 are entered");
          System.out.println("Please Enter Valid Inputs ");
      }
      else{
          printElements(arr,n);
      }                 
   }
}


Enter the total number of elements 10
Enter the elements of the array 0 0 1 1 1 1 1 0 0 0
Resultant Array is
0 0 0 0 0 1 1 1 1 1

Program 2: Separate 0s on the left side and 1s on the right side

In this method, we will see how to separate 0s on the Left side and 1s on the right side of the array using the segregation technique.

Algorithm

  1. Start
  2. Declare the array size.
  3. Ask the user to initialize the array size.
  4. Declare the array.
  5. Ask the user to initialize the array elements.
  6. First check whether the elements entered are 0 and 1 or not.
  7. If elements entered are other than 0 and 1 ask the user to enter again.
  8. If elements entered are 0 and 1 then declare a variable to count the total number of zeros.
  9. Use a for loop to iterate through each element of the array.
  10. Increment the count wherever 0 is found.
  11. Now, fill the loop with 0s till count.
  12. Fill the remaining elements of the array with 1s.
  13. Stop

The below program demonstrates how to separate 0s on the Left side and 1s on the right side of the array using the segregation technique.

import java.util.*;  
import java.util.Arrays; 

//Driver Code
public class Main  
{  
    static void printElements(int arr[],int n)
    {
        System.out.println("Resultant Array is ");
        for(int i=0;i<n;i++)
        {
            System.out.print(arr[i]+" ");
        }
        System.out.println(" ");
    }
   public static void main(String args[])   
   {  
       Scanner sc=new Scanner(System.in);

      int n;    //Declare array size
      System.out.println("Enter the total number of elements ");
      n=sc.nextInt();     //Initialize array size

      int arr[]=new int[n];   //Declare array
      System.out.println("Enter the elements of the array ");
      for(int i=0; i<n ;i++)     //Initialize array
      {
          arr[i]=sc.nextInt();
      }
      
      int flag=1;
      
        for(int t=0;t<n;t++)
        {
            if(arr[t]==0 || arr[t]==1)
            {
               // Counts the no of zeros in array 
               int count = 0; 
  
               // Iteration over each element of the array 
               for (int i = 0; i < n; i++) 
               { 
                    if (arr[i] == 0) 
                    count++;       // Incrementing the count 
               } 
  
             // Loop to fill the array with 0 until count 
             for (int i = 0; i < count; i++) 
             arr[i] = 0; 
  
             // Loop to fill the remaining array space with 1 
             for (int i = count; i < n; i++) 
                arr[i] = 1; 
             
           flag++;
          }
          else
          {
                 flag=0;
          }
      }
      
      if(flag==0)
      {
          System.out.println("Elements other than 0 and 1 are entered");
          System.out.println("Please Enter Valid Inputs ");
      }
      else
      {
          printElements(arr,n);
      }
      
   }
}


Enter the total number of elements 10
Enter the elements of the array 0 0 1 1 1 0 1 1 0 0
Resultant Array is
0 0 0 0 0 1 1 1 1 1



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.