|
1 |
| -**Algorithmic Steps** |
2 |
| -This problem is solved with the help of two pointers technique. It is also know as "Dutch National Flag problem". The algorithmic approach can be summarized as follows: |
| 1 | +**Description:** |
| 2 | +Given an array `nums` with `n` objects colored red, white, where each object is color coded as integer: blue, 0 for red, 1 for white, and 2 for blue. We need to sort them in-place so that objects of the same color are grouped together, with the colors in the order red, white, and blue. |
3 | 3 |
|
| 4 | +This problem needs to be solved without using the library's sort function. |
4 | 5 |
|
5 |
| -1. Initialize left and right pointers to first(i.e, 0) and end index of the array, to keep track of the current window boundaries. |
| 6 | +### Examples |
6 | 7 |
|
7 |
| -3. Initialize current index pointer(i.e, i) to 0, to keep track of the current character while iterating the array. |
| 8 | +Example 1: |
8 | 9 |
|
9 |
| -4. Iterate over the input array using index pointer until the end of the array. |
| 10 | +Input: nums = [2,1,0,1,2,0] |
| 11 | +Output: [0,0,1,1,2,2] |
10 | 12 |
|
11 |
| -5. If the current character is equals to 0, swap the character values at left pointer and index pointer. Also, increment the left pointer and index pointer. |
| 13 | +Example 2: |
12 | 14 |
|
13 |
| -6. If the current character is equals to 2, swap the character values at right pointer and index pointer. Also, decrement the right pointer. If the chracter is neither 0 or 2, then just increment the index pointer. |
| 15 | +Input: nums = [2,1,0] |
| 16 | +Output: [0,1,2] |
14 | 17 |
|
15 |
| -8. Repeat steps 5–6 until the index pointer reaches the end of the array. |
| 18 | +**Algorithmic Steps:** |
16 | 19 |
|
17 |
| -9. Return the updated in-place array where chracters are sorted. |
| 20 | +This problem is solved with the help of **two pointers** technique. It is also know as **"Dutch National Flag problem"**. The algorithmic approach can be summarized as follows: |
| 21 | + |
| 22 | + |
| 23 | +1. Initialize both left and right pointers to first index(i.e, `0`) and end index(i.e, `nums.length-1`) of the array respectively, to keep track of the current window boundaries. |
| 24 | + |
| 25 | +2. Initialize current index pointer(i.e, `i`) to 0, to keep track of the current character while iterating the array. |
| 26 | + |
| 27 | +3. Iterate over the input array using index pointer until the end of the array. |
| 28 | + |
| 29 | +4. If the current character is equals to 0, swap the character values at left pointer and index pointer. Also, increment the left pointer and index pointer. |
| 30 | + |
| 31 | +5. If the current character is equals to 2, swap the character values at right pointer and index pointer. Also, decrement the right pointer. |
| 32 | + |
| 33 | +6. If the chracter is neither `0` or `2`, then just increment the index pointer. |
| 34 | + |
| 35 | +7. Repeat steps 4–6 until the index pointer reaches the end of the array. |
| 36 | + |
| 37 | +8. Return the updated in-place array where chracters are sorted. |
18 | 38 |
|
19 | 39 | **Time and Space complexity:**
|
20 |
| -This algorithm has a time complexity of O(n) because we are traversing the array only once. Also, it takes requires space complexity of O(1) because we are updating the array in-place without using an additional array. |
| 40 | +This algorithm takes a time complexity of `O(n)` because we are traversing the array only once. Also, it requires space complexity of `O(1)` because we are updating the array in-place without using an additional data structure. |
0 commit comments