Java: Segregate all 0s on left side and all 1s on right side of a given array of 0s and 1s
Write a Java program to separate 0s and 1s in an array of 0s and 1s into left and right sides.
Pictorial Presentation:
Sample Solution:
Java Code:
// Import necessary Java libraries.
import java.util.*;
import java.lang.*;
// Define a class named Main.
public class Main
{
// The main method for executing the program.
public static void main(String[] args)
{
// Define an array of integers containing 0s and 1s.
int nums[] = {0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1};
int i, nums_size = nums.length;
int left = 0, right = nums_size - 1;
// Print the original array.
System.out.println("Original Array : " + Arrays.toString(nums));
// Perform segregation of 0s and 1s in the array.
while (left < right)
{
/* While 0 at the left, increment the left index. */
while (nums[left] == 0 && left < right)
left++;
/* While we see 1 at the right, decrement the right index. */
while (nums[right] == 1 && left < right)
right--;
if (left < right)
{
// Swap 0 and 1 and increment left and decrement right.
nums[left] = 0;
nums[right] = 1;
left++;
right--;
}
}
// Print the array after segregation.
System.out.println("Array after segregation is : " + Arrays.toString(nums));
}
}
Sample Output:
Original Array : [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1] Array after segregation is : [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
Flowchart:
Java Code Editor:
Previous: Write a Java program to find smallest and second smallest elements of a given array.
Next: Write a Java program to find all combination of four elements of an given array whose sum is equal to a given value.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics