Dutch National Flag Problem
Tags: computerscience, 100daysofcode, java
Published: 2021-01-17 22:56:56

I was going through LeetCode problems that I have solved, looking for one that would be good to write a post about. I came across the Dutch National Flag Problem and knew right away that it would be a good one.

LeetCode says that I solved this one on December 12, 2020 at 18:04. I remember this one because I was able to solve it all on my own. Being able to solve a problem all by yourself, certainly is powerful positive reinforcement. I certainly can use a bit of a pick-me-up after last night’s [LeetCode Contest](https://leetcode.com/contest/). I was only able to answer one question! Oh well, there is always next week.

Before we get into the problem, I just wanted to give some background on it. It was actually first created by Edsger W. Dijkstra. Pretty neat if you ask me. Read more about it [here](https://en.wikipedia.org/wiki/Dutch_national_flag_problem).

## The Problem

Given an array nums with n objects colored red, white, or blue, sort them [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) 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.

## Examples

**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]

**Example 3:
Input:** nums = [0]
**Output:** [0]

**Example 4:
Input:** nums = [1]
**Output:** [1]

[Link to Problem](https://leetcode.com/problems/sort-colors/)

## How to Solve

A good way to think about this problem is to realize that there are three different possible numbers (or colors) that can be in the array. As the array is iterated through, a different action needs to occur for each of the different numbers.

The twos need to all be at the right end of the output array, the zeros need to be all at the left end of the output array, and the ones need to be in the middle.

One of my initial thoughts for this problem was that there would have to be multiple pointers. One pointer to keep track of the lower bound, one for the upper bound, and then one that iterates. The pointers for the bounds will only increment or decrement, when the position that corresponds to that pointer is sorted properly.

Here are the steps:

1. If the current number is zero, then swap the current number with the number at the lower bound. Increase the lower bound and current index by one.

1. If the current number is one, increase only the current index by one.

1. If the current number is two, swap the current number with the number at the upper bound. Decrease the upper bound by one.

1. Repeat that process until the index for current passes the upper bound index value.

## The Code

Here is the code that I came up with. Might not be a one-liner, but I still am pretty proud of it. If you want to see some other solutions, here is a [top-rated one](https://leetcode.com/problems/sort-colors/discuss/26500/Four-different-solutions) and here is another [top-rated one](https://leetcode.com/problems/sort-colors/discuss/26472/Share-my-at-most-two-pass-constant-space-10-line-solution) too.

{% gist
https://gist.github.com/djl218/f2fd0bc7cd9f0ab3f0bd5eedd5096943 %}

## Some Illustrations

I find illustrations pretty helpful to better understand the steps involved in an algorithm. Let’s look at some illustrations to help us be able to see this algorithm in action.

**Input:** [2, 1, 0, 0, 1, 2]

Figure A shows the initial setup. The left is 0, current is 0, and right is 5. nums[current] = 2 and nums[right] = 2, so just right is decremented by one.

![Figure A](https://cdn-images-1.medium.com/max/2000/1*j0b0n59Vm2acKgkEyFHvbQ.png)<center>*Figure A*</center>

In Figure B, once again nums[current] = 2, but this time nums[right] = 1. These two values will be swapped. right is decreased by one.

![Figure B](https://cdn-images-1.medium.com/max/2000/1*Kk6FkBF3nRP9Mz1kRFcplw.png)<center>*Figure B*</center>

In Figure C, nums[current] = 1, so only current is increased by one.

![Figure C](https://cdn-images-1.medium.com/max/2138/1*1jyoguQ9xIyMWJDhMOmttw.png)<center>*Figure C*</center>

In Figure D, nums[current] = 1, so current is again increased by one.

![Figure D](https://cdn-images-1.medium.com/max/2118/1*4KMZipZS4Zt7USHd79nT8A.png)<center>*Figure D*</center>

In Figure E, nums[current] = 0, so nums[current] is swapped with nums[left] and then both left and current are increased by one.

![Figure E](https://cdn-images-1.medium.com/max/2094/1*DzbTqDoXEHDIokyuqqQZSw.png)<center>*Figure E*</center>

In Figure F, nums[current] = 0, so nums[current] is swapped with nums[left]. left and current are increased by one. currrent is no longer less than or equal to right, so the while loop terminates and nums is returned as output.

![Figure F](https://cdn-images-1.medium.com/max/2110/1*KgrjufQ5kFRoOL7ytx6UPg.png)<center>*Figure F*</center>

```code
result: [0, 0, 1, 1, 2, 2]
```

## Time Complexity

If ’n’ is the number of elements in the input array, then the time complexity of this algorithm is O(n), as the algorithm only requires one pass.

## Space Complexity

The space complexity of the algorithm is O(1). The algorithm runs in constant space.

## Get Better at Algorithms!

Algorithms and data structures are pretty tough. They are definitely taking a while for me to get the hang of them. However, there are some great resources out there, and I feel obligated to share some that have been most helpful to me. If I missed any that have been helpful to you, be sure to mention them in the comments.

* **Cracking the Coding Interview** — Great resource. Really gets you in the right mindset for interviews. You can find it [here](https://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/0984782850).

* **Elements of Programming Interviews** — Another great book. Personally, I like this one more than CTCI, but YMMV. You can find it [here](https://www.amazon.com/Elements-Programming-Interviews-Java-Insiders/dp/1517671272/ref=pd_lpo_14_t_0/134-2745636-3821839?_encoding=UTF8&pd_rd_i=1517671272&pd_rd_r=4eebd030-1368-436b-9bdd-22a403a57eae&pd_rd_w=ku4HH&pd_rd_wg=qY5q5&pf_rd_p=7b36d496-f366-4631-94d3-61b87b52511b&pf_rd_r=Y44KR67YH071M3P9JGSH&psc=1&refRID=Y44KR67YH071M3P9JGSH).

* **Grokking the Coding Interview** — Can’t emphasize this one enough. Haven’t seen it mentioned it too often. Explains patterns that occur in different coding challenge problems. Great at providing a big-picture of all the different algorithm problems you might encounter. Check it out [here](https://www.educative.io/courses/grokking-the-coding-interview).

* **Grokking Dynamic Programming** — Dynamic programming is tough. This course has definitely helped me get a better understanding.

* **Tushar** **Roy** — Tushar really knows his stuff. His dynamic programming playlist is especially good. Check out his awesome [YouTube channel](https://www.youtube.com/user/tusharroy2525/featured).

* **Back To Back SWE** — Great [YouTube Channel](https://www.youtube.com/channel/UCmJz2DV1a3yfgrR7GqRtUUA). Highly recommend.

* **Kevin Naughton Jr.** — Another awesome [YouTube channel](https://www.youtube.com/channel/UCKvwPt6BifPP54yzH99ff1g). Great at going over problems and gives helpful advice.

* **Base CS** — Vaidehi Joshi does a great job of laying out the fundamentals of algorithms and data structures. Check out the blog series [here](https://medium.com/basecs). She also has a [podcast](https://www.codenewbie.org/basecs) that I give two thumbs up.

* **Coding Challenge Website** — There are plenty of different ones to choose from. [HackerRank](https://www.hackerrank.com/), [CodeWars](https://www.codewars.com/), and [Edabit](https://edabit.com/) all seem to be pretty popular. I personally use [LeetCode](https://leetcode.com/). Find the one that works for you!

* **Pramp** — Do mock interviews! The sooner the better! [Pramp](https://www.pramp.com/) has been a huge help to me.

Well, I hope that was useful. Thanks for reading my post and best of luck with your learning about data structures and algorithms!