# 75. Sort Colors/ Dutch National Flag

## Problem Statement

\
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.

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

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [2,0,2,1,1,0]
</strong><strong>Output: [0,0,1,1,2,2]
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [2,0,1]
</strong><strong>Output: [0,1,2]
</strong></code></pre>

&#x20;

**Constraints:**

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

&#x20;

## Intuition

```
"New technique learned 
        Dutch National flag algorithm

Basically, 3 pointers -> 
low : 0's should be to left of it
high : 2's should be to right of it

Mid  pointer does swaps


"
```

### Links

<https://leetcode.com/problems/sort-colors/>

### Video Links

<https://www.youtube.com/watch?v=oaVa-9wmpns&list=PLgUwDviBIf0rPG3Ictpu74YWBQ1CaBkm2&index=3>

### Approach 1:

```
Dutch National Flag Algorithm
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    void sortColors(vector<int>& nums) {
        int low=0;
        int mid=0;
        int high=nums.size()-1;
        
        while(mid<=high){
            if(nums[mid]==0)
                swap(nums[mid++],nums[low++]);
            else if(nums[mid]==1)
                mid++;
            else
                swap(nums[mid],nums[high--]);
        }
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/array/medium/75.-sort-colors-dutch-national-flag.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
