Professional Documents
Culture Documents
● Tutorials
● Student
● Jobs
● Courses
● Write
● Practice
●
● Premium
● Jobs
○ Geeks Digest
○ Quizzes
○ Geeks Campus
○ Gblog Articles
○ IDE
○ Campus Mantri
● Sign In
● Sign In
● Home
● Courses
○ Algorithms
○ Analysis of Algorithms
○ Data Structures
○ Interview Corner
○ Languages
○ ISRO CS
○ GATE
○ CS Subjects
○ Web Technologies
○ School Learning
○ Mathematics
○ RD Sharma Solutions
○ UGC NET CS
○ Student
○ Jobs
● GBlog
● Puzzles
● What's New ?
Change Language
Related Articles
Related Articles
●
● Segregate Even and Odd numbers
● Sort all even numbers in ascending order and then sort all odd numbers in
descending order
● Sort even-placed elements in increasing and odd-placed in decreasing order
● Permute two arrays such that sum of every pair is greater or equal to K
minimized
● Sort an array when two halves are sorted
both array
● Count items common to both the lists but with different prices
● Find pairs with given sum such that elements of pair are in different rows
● Arrays in Java
● Arrays in C/C++
● Given an array A[] and a number x, check for pair in A[] with sum as x
left side and 1s on right side of the array [Basically you have to sort the
to the solution.
traverse the entire array and find out the number of zeros in the array, In
this case the number of zeros is 4 so now we can easily get the number
2) Once we have counted, we can fill the array first we will put the zeros
and then ones (we can get number of ones by using above formula).
Time Complexity : O(n)
● C++
● Java
● Python3
● C#
● PHP
● Javascript
class GFG {
count++;
arr[i] = 0;
arr[i] = 1;
{
System.out.print("Array after segregation is ");
// driver function
int n = arr.length;
segregate0and1(arr, n);
print(arr, n);
}
// This code is contributed by Kamal Rawal
Output :
Method 1 traverses the array two times. Method 2 does the same in a
single pass.
right as n-1.
Implementation:
● C++
● C
● Java
● Python
● C#
● PHP
● Javascript
class Segregate
right--;
left
arr[right]*/
arr[left] = 0;
arr[right] = 1;
left++;
right--;
seg.segregate0and1(arr, arr_size);
Output:
Another approach :
● C++
● Java
● Python3
● C#
● PHP
● Javascript
import java.util.*;
class GFG{
/**
int type0 = 0;
if (arr[type0] == 1) {
// swap
arr[type0] = arr[type1]-arr[type0];
arr[type1] = arr[type1]-arr[type0];
type1--;
} else {
type0++;
}
}
// Driver program
segregate0and1(array);
for(int a : array){
System.out.print(a+" ");
}
Output:
In case you wish to attend live classes with experts, please refer DSA
Like0
Previous
Next
RECOMMENDED ARTICLES
Page :
1
2
30, Apr 21
07, Jun 10
22, Apr 11
Segregate even and odd numbers | Set 2
01, Feb 18
01, Nov 20
13, May 18
Modify array to another given array by replacing array elements with the sum of the array
12, Feb 21
Minimize swaps required to place largest and smallest array elements at first and last array indices
28, Jan 21
09, Sep 20
23, Jul 21
12, Apr 17
Check whether an array can be fit into another array
rearranging the elements in the array
09, Jul 18
24, Mar 20
01, Oct 20
27, Oct 20
Reduce array to longest sorted array possible by
removing either half of given array in each operation
17, Nov 20
21, Dec 20
03, May 21
07, Sep 17
20, Jul 21
12, Jul 21
Sum of Bitwise AND of sum of pairs and their Bitwise
AND from a given array
23, Feb 21
10, Nov 14
Article Contributed By :
GeeksforGeeks
EasyNormalMediumHardExpert
Improved By :
● nik1996
● Shivi_Aggarwal
● shrikanth13
● rathbhupendra
● surbhityagi15
● subham348
● subhammahato348
● namant744
Article Tags :
● Accolite
● Amazon
● Fab.com
● MakeMyTrip
● Paytm
● Zoho
● Arrays
● Sorting
Practice Tags :
● Paytm
● Zoho
● Accolite
● Amazon
● MakeMyTrip
● Fab.com
● Arrays
● Sorting
Improve Article
Report Issue
WHAT'S NEW
View Details
DSA Self Paced Course
View Details
View Details
● Linear Search
● Merge Sort
● Bubble Sort
● QuickSort
● Insertion Sort
● Selection Sort
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
● Company
● About Us
● Careers
● Privacy Policy
● Contact Us
● Copyright Policy
● Learn
● Algorithms
● Data Structures
● Languages
● CS Subjects
● Video Tutorials
● Practice
● Courses
● Company-wise
● Topic-wise
● How to begin?
● Contribute
● Write an Article
● Internships
● Videos
have read and understood our Cookie Policy & Privacy PolicyGot It !