Challenge: Dutch National Flag Problem

Let's solve the very famous problem of Dutch national flag.

Problem statement

Implement a function that sorts an array of 0s0's, 1s1's and 2s2's. This is called the Dutch national flag 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!


The input is an array of 0s0's, 1s1's and 2s2's


The output is a printed 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 70+ hands-on prep courses.