Sort (Segregate) 0s 1s and 2s in an array

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

Segregate 0s 1s and 2s in an array. Arrange all zeros in first half, all ones in second half and all twos in third half.

Given an array with 0's, 1's and 2's, we are segregating the 0's, 1's and 2's.



0 1 2 0 1 1 1 1 1 0 1 2 1 0 2 2 2 0 1 2 0 1


0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2



1.    Simply create an count_array of size 3 and initialize all the element of the array to be zero.
2.    The array element of count_array depicts the number of occurences of index i.
3.    Traverse the given array and if we get 0 increment the 0th index of count_array and similarly do for 1 and 2 by incrementing the value count_array of 1st and 2nd index.
4.    Finally print the number of 0’s , 1’s and 2’s as many times as the value of element of count_array.

C++ Program

#include <bits/stdc++.h>
using namespace std;
int main()
	int arr[] = {1,0,2,1,0,1,2,0,1,2,0,1,1,1,0,2,0,2,2,0,1,2};//array
	int N = sizeof(arr)/sizeof(arr[0]);//size of array
	int cnt[3]={0};//hshing approach
	for(int i=0;i<N;i++)
		cnt[arr[i]]++;//increase the count of that index 
	for(int i=0;i<3;i++)
		for(int j=0;j<cnt[i];j++)//print that index as many times its value is
			cout<<i<<" ";
	return 0;
Try It



1.    Simply sort the array (using either Heap/Merge sort or using sort() STL function).
2.    Simply print the sorted array.

C++ Program

#include <bits/stdc++.h>
using namespace std;
int main()
	int arr[] = {1,0,2,1,0,1,2,0,1,2,0,1,1,1,0,2,0,2,2,0,1,2};
	int N = sizeof(arr)/sizeof(arr[0]);
	sort(arr,arr+N);//sort the array simply
	for(int i=0;i<N;i++)
		cout<<arr[i]<<" ";
	return 0;
Try It

C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top