Segregate 0s and 1s in an Array – Java Code

Segregate 0s and 1s in an Array. Given an array of 0s and 1s in random order. Write a java program to segregate 0’s on left side and 1’s on right side of the array.

For example –

Input array = [0, 1, 0, 1, 0, 0, 1]
Output array = [0, 0, 0, 0, 1, 1, 1]

Segregate 0s and 1s in an array

Segregate 0s and 1s in an array

To solve this problem. We traverse an array and move 0’s on left side then we move 1’s on right side in a single traversal. The time complexity of this approach is O(n).

Subscribe Our Tutorials

Get Latest Updates on Facebook

Java Program to Segregate 0s and 1s in an Array

In this coding example, I have explained how we can segregate 0s and 1s in a single traversal of an array. For better explanation, i have mention video tutorial link at the end of this post.

 

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.