Segregate 0s and 1s Problem in Java6 May 2025 | 4 min read It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Flipkart etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to find the Separate 0s and 1s problem with different approaches and logic. Also, we will create Java programs for the same. The Separate 0s and 1s problem simply rearranges the array of 0s and 1s so that all 0s appear on one side. (usually on the left) and both 1 will appear on the other side. This problem is often considered a form of the two-pecker technique and is a fundamental question of array manipulation. This problem is essential for understanding basic sorting techniques, working with limited values in arrays, and implementing in-place algorithms with minimal time complexity. ExampleInput Array: {0, 1, 1, 0, 1, 0, 0, 1, 1, 0} Initial State:
Iteration 1:
Iteration 2:
Iteration 3:
End:
Approaches to Solve the Problem
File Name: SegregateZerosAndOnes.java Output: Original Array: 0 1 1 0 1 0 0 1 1 0 Array after segregation: 0 0 0 0 0 1 1 1 1 1 ExplanationThe given Java program segregates 0s and 1s in an array using the two-pointer technique, ensuring an efficient and in place solution. It initializes two pointers, left at the start and right at the end of the array. The left pointer moves forward facing 0 seconds because it is in the correct position. and the right pointer moves backward facing for 1 second. When arr[left] is 1 and arr[right] is 0, both values are switched to their correct positions. and both pointers will move inward. This process continues until the pointers meet, resulting in all 0s on the left and all 1s on the right. This approach guarantees a time complexity (O(1)) with space (O(1)), making it highly efficient and suitable for binary arrays. Advantages of the TwoPointer Technique
Use Cases
Complexity Analysis1. Time ComplexityThe algorithm traverses the array once, making comparisons and swaps in constant time for each element.
2. Space ComplexityThe algorithm operates entirely inplace, requiring no additional memory.
3. Optimized EfficiencyThe constant space and linear time complexity make the algorithm highly efficient, especially when working with large arrays. It avoids the overhead of using extra data structures, offering a performance advantage in memoryconstrained systems. ConclusionThe "Segregate 0s and 1s" problem demonstrates the power of inplace sorting using the twopointer technique. It efficiently rearranges a binary array without requiring additional space, ensuring all 0s are positioned to the left and all 1s to the right. The approach is simple yet optimal, making it suitable for both beginner and advanced programmers. By minimizing traversals and swaps, it highlights the importance of optimizing algorithms for time and space complexity, especially for large datasets. This solution is robust and can serve as the foundation for solving more complex segregation problems. Next TopicKruskal Algorithm Java |
Java Generics Jenkov
Java is a versatile and widely-used programming language, celebrated for its object-oriented nature and platform independence. One of the key features that make Java such a powerful tool for developers is its support for generics. Generics in Java allow you to create classes, interfaces, and methods...
4 min read
What is new in Java 17
Java 17 was made available in September 2021, replacing Java 11 as the most recent LTS (long-term support). The crucial query right now is, "?" JDK (14) Enhancement Proposals (JEP) items are included in Java 17. Ten of those are new features, two are being removed, and...
19 min read
Counting Equal Pairs in a String in Java
To determine the number of equal pairs in a string, locate all instances where the same characters appear in various locations inside the text. A pair is deemed "equal" if two characters are identical but appear at separate indices. The aim is to determine how many...
5 min read
Empty Statement in Java
In Java, there are three types of statements that are declaration, expression, and control statements. Beside this, there is another statement is known as empty statement. In this section, we will discuss about the empty statement in Java with examples. Empty Statement As the name suggests, an empty...
4 min read
Balanced Prime Number in Java
In this section, we will discuss what is balance prime number and how to find balanced prime number through a Java program. Balance Prime Number A balanced prime number is a prime number that is equal to the average of the immediate and immediate ious prime numbers. Let's...
5 min read
Binary to Gray Code Using Recursion in Java
Named for Frank Grey, Grey Code is a binary numeral system in which there is just a single bit difference between two successive values. It is also known as the "Reflected Binary Code" because the (n-1)-bit form of it may be reflected and appended to itself...
4 min read
String.valueOf() Method in Java
In Java, the String.valueOf() method is an overloaded and static method that facilitates the conversion of a variety of data types, including objects, booleans, floats, double, long, and integers, into their string representations. It makes working with string manipulation, logging, and effectively displaying data easy. Overloaded...
5 min read
Wap to Reverse a String in Java using Lambda Expression
In Java, a String is a sequence of characters that remains immutable once created. If there's a need to reverse a user-entered string, the `charAt()` method from the String class can be employed. The method facilitates the extraction of individual characters from the string, enabling their...
3 min read
Gregorian calendar Java
A concrete subclass of the Calendar class is referred to as GregorianCalendar. The GregorianCalendar class has an implementation of all of its inherited members. The Calendar class implements the mostly used Gregorian calendar. In order to use the Gregorian calendar in Java, we import the Java.util.GregorianCalendar...
16 min read
Java Password Generator
Usually, we need to generate a secure password for security purposes. There are several ways to generate a strong password in Java. In this section, we will understand how we can generate a strong password having at least two lowercase characters, two uppercase characters, two digits,...
8 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India