Java Program to Separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in Random Order

In the previous article, we have seen Java Program to Arrange the Elements of a Given Array of Integers Where All Negative Integers Appear Before All the Positive Integers

In this article we are going to see how to separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in random order using Java programming language..

Java Program to Separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in Random Order

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in random order.

Method-1: Java Program to Separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in Random Order By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Initialize two pointers, i = 0, j = arr.length – 1.
  • While i <= j, if the element at i is equal to 0, increment i.
  • If the element at j is equal to 1, decrement j.
  • Now at index i, there is 1 and at index j, there is 0, so swap these two.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        int[] nums = { 0, 1, 0, 0, 1, 1, 0, 0, 1 };
        System.out.print("Original array: ");
        
        for (int i : nums)
        {
            System.out.print(i + " ");
        }
        
        modifyMethod(nums);
        System.out.print("\nModified array: ");
        for (int i : nums) {
            System.out.print(i + " ");
        }

    }

    static void modifyMethod(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        while (i <= j) {
            // if the element at i is negative, increment i
            if (nums[i] == 0 )
                i++;
            // if the element at j is positive, increment j
            if (nums[j] == 1)
                j--;
            // swap the elements
            if (i <= j) {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
    }
}

Output:

Original array: 0 1 0 0 1 1 0 0 1 
Modified array: 0 0 0 0 0 1 1 1 1

Method-2: Java Program to Separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in Random Order By Dynamic Initialization of Array Elements

Approach:

  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Initialize two pointers, i = 0, j = arr.length – 1.
  • While i <= j, if the element at i is equal to 0, increment i.
  • If the element at j is equal to 1, decrement j.
  • Now at index i, there is 1 and at index j, there is 0, so swap these two.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        // asking user to enter the number of elements
        System.out.println("Enter number of elements in the array: ");
        int n = sc.nextInt();
        // initializing the array
        int[] nums = new int[n];
        // asking user to enter the elements
        System.out.println("Enter elements of the array: ");
        for (int i = 0; i < n; i++) 
        {
            nums[i] = sc.nextInt();
        }

        System.out.print("Original array: ");
        for (int i : nums) 
        {
            System.out.print(i + " ");
        }
        
        //calling modifyMethod() method
        modifyMethod(nums);
        System.out.print("\nModified array: ");
        for (int i : nums) 
        {
            System.out.print(i + " ");
        }

    }

    static void modifyMethod(int[] nums) 
    {
        int i = 0;
        int j = nums.length - 1;
        while (i <= j) {
            // if the element at i is negative, increment i
            if (nums[i] == 0 )
                i++;
            // if the element at j is positive, increment j
            if (nums[j] == 1)
                j--;
            // swap the elements
            if (i <= j) {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
    }
}

Output:

Enter number of elements in the array: 
5
Enter elements of the array: 
1 0 1 0 1 1
Original array: 1 0 1 0 1 
Modified array: 0 0 1 1 1

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs: