Java: Segregate all 0s on left side and all 1s on right side of a given array of 0s and 1s
42. Separate 0s and 1s in an 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:
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:
For more Practice: Solve these Related Problems:
- Write a Java program to move all negative numbers to one side of an array.
- Write a Java program to sort an array consisting only of 0s, 1s, and 2s without using the sort function.
- Write a Java program to find the minimum number of swaps required to group all 1s together in a binary array.
- Write a Java program to count the number of subarrays where the number of 0s and 1s is equal.
Go to:
PREV : Find smallest and second smallest in an array.
NEXT : Find all combinations of four elements summing to target.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.