Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

75. Sort Colors

Medium65.6% Acceptance
ArrayTwo PointersSorting
Asked by:
M
Microsoft
ProblemHints (3)Solutions (12)VideosCompanies (10)Notes

Problem Statement

Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.

You must solve this problem without using the library's sort function.

Example 1:

Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Example 2:

Input: nums = [2,0,1]
Output: [0,1,2]

Constraints:

  • n == nums.length
  • 1 <= n <= 300
  • nums[i] is either 0, 1, or 2.

Follow up: Could you come up with a one-pass algorithm using only constant extra space?

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Amazon
V
VMware
A
Adobe
S
Salesforce
+5

Approach

The Sort Colors problem asks you to reorder an array containing only 0, 1, and 2 so that elements of the same color are grouped together. A straightforward idea is counting occurrences of each value and rewriting the array. While simple, this approach requires two passes over the array.

A more optimal and commonly discussed solution is the Dutch National Flag algorithm. This method uses three pointers to partition the array into three regions representing 0s, 1s, and 2s. By adjusting pointers and swapping elements in a single traversal, the array can be sorted in-place.

This approach is preferred in interviews because it demonstrates mastery of the two-pointer technique and in-place array manipulation. The algorithm runs in linear time and requires constant extra space, making it both efficient and elegant for this problem.

Complexity

ApproachTime ComplexitySpace Complexity
Counting / Frequency MethodO(n)O(1)
Dutch National Flag (Three Pointers)O(n)O(1)

Video Solution Available

NeetCode

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

A rather straight forward solution is a two-pass algorithm using counting sort.

2
Hint 2

Iterate the array counting number of 0's, 1's, and 2's.

3
Hint 3

Overwrite array with the total number of 0's, then 1's and followed by 2's.

Ready to see the solutions?View Solutions

Solutions (12)

Counting Sort Approach

This approach involves counting the occurrences of each color and then overwriting the array with the correct number of zeros, ones, and twos. This is straightforward and efficiently utilizes fixed positions for each of the colors.

Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(1), as it uses a fixed-size array.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3void sortColors(int* nums, int numsSize) {
4    int count[3] = {0};
5    for (int i = 0; i < numsSize; i++) {
6        count[nums[i]]++;
7    }
8    int index = 0;
9    for (int i = 0; i < count[0]; i++)
10        nums[index++] = 0;
11    for (int i = 0; i < count[1]; i++)
12        nums[index++] = 1;
13    for (int i = 0; i < count[2]; i++)
14        nums[index++] = 2;
15}
16
17int main() {
18    int nums[] = {2,0,2,1,1,0};
19    int size = sizeof(nums) / sizeof(nums[0]);
20    sortColors(nums, size);
21    for (int i = 0; i < size; i++)
22        printf("%d ", nums[i]);
23    return 0;
24}

Explanation

The function sortColors initializes a count array with three elements set to zero. It first iterates through the given array to count the number of 0s, 1s, and 2s. Then, it rewrites the original array based on these counts, placing the correct number of 0s, 1s, and 2s in order.

Dutch National Flag Problem Solution

This approach, modeled after the Dutch National Flag Problem, uses three pointers to partition the array into sections for 0s, 1s, and 2s. It performs a one-pass in-place sort by maintaining these fixed sections dynamically as it traverses the array.

Time Complexity: O(n), where n is the length of the array.
Space Complexity: O(1), because we only use fixed pointers.

CC++JavaPythonC#JavaScript
1#

Video Solutions

Watch expert explanations and walkthroughs

Sort Colors - Quicksort Partition - Leetcode 75 - Python

NeetCode
15:48106,865 views

Asked By Companies

10 companies
M
Microsoft
A
Amazon
V
VMware
A
Adobe
S
Salesforce
N
Nvidia
G
Grab
U
Uber
F
Facebook
I
Intel

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
3SumMedium
3Sum ClosestMedium
4SumMedium
More similar problems

Related Topics

ArrayTwo PointersSorting

Problem Stats

Acceptance Rate65.6%
DifficultyMedium
Companies10

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Sort Colors asked in FAANG interviews?

Yes, Sort Colors is a common interview problem and has appeared in interviews at top tech companies. It tests understanding of array manipulation, two-pointer techniques, and in-place sorting strategies.

What is the optimal approach for Sort Colors?

The optimal approach is the Dutch National Flag algorithm. It uses three pointers to partition the array into sections for 0s, 1s, and 2s while traversing the array once. This allows the array to be sorted in-place with O(n) time and O(1) space.

What data structure is best for solving Sort Colors?

The problem is primarily solved using arrays with pointer manipulation. No additional data structures are required because the algorithm rearranges elements directly within the input array using swaps.

Why is the Dutch National Flag algorithm used in Sort Colors?

The Dutch National Flag algorithm efficiently partitions elements into three groups using a single pass. Since the array contains only three distinct values, the algorithm can place each element in its correct region without needing extra memory.

Previous Problem

Search a 2D Matrix

Next Problem

Subsets

include
<stdio.h>
2
3
void
sortColors
(
int
*
nums
,
int
numsSize
)
{
4
int
low
=
0
,
mid
=
0
,
high
=
numsSize
-
1
;
5
while
(
mid
<=
high
)
{
6
if
(
nums
[
mid
]
==
0
)
{
7
int
temp
=
nums
[
low
]
;
8
nums
[
low
]
=
nums
[
mid
]
;
9
nums
[
mid
]
=
temp
;
10
low
++
;
mid
++
;
11
}
else
if
(
nums
[
mid
]
==
1
)
{
12
mid
++
;
13
}
else
{
14
int
temp
=
nums
[
mid
]
;
15
nums
[
mid
]
=
nums
[
high
]
;
16
nums
[
high
]
=
temp
;
17
high
--
;
18
}
19
}
20
}
21
22
int
main
(
)
{
23
int
nums
[
]
=
{
2
,
0
,
2
,
1
,
1
,
0
}
;
24
int
size
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
25
sortColors
(
nums
,
size
)
;
26
for
(
int
i
=
0
;
i
<
size
;
i
++
)
27
printf
(
"%d "
,
nums
[
i
]
)
;
28
return
0
;
29
}

Explanation

In this C implementation, three pointers are maintained: low, mid, and high. The pointer low denotes the boundary for zeros, mid processes each element, and high indicates the start of twos. As the algorithm proceeds, swaps are made to ensure elements in positions up to low are 0s, up to mid are 1s, and post-high are 2s.