Challenge 10: Dutch National Flag Problem

Problem Statement

Implement a function that sorts an array of 0s0's, 1s1's and 2s2's. This is called the Dutch National Problem. Since the number OO can be represented as the blue color, 11 as the white color, and 22 as the red color, we want the array to form the Dutch flag.

Try solving this problem inplace and in linear time without using any extra space!


An array of 0s0's, 1s1's and 2s2's


An array where the numbers 00, 11, and 22 are sorted.

Sample Input

arr = {2, 0, 0, 1, 2, 1, 0}

Sample Output

0 0 0 1 1 2 2

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.