Difficulty | Source | Tags | ||
---|---|---|---|---|
Easy |
160 Days of Problem Solving |
|
The problem can be found at the following link: Problem Link
You are given an array arr[]
containing only 0s, 1s, and 2s. The task is to sort the array in ascending order. The problem must be solved in-place, and the space complexity should be constant.
Input:
arr[] = [0, 1, 2, 0, 1, 2]
Output:
[0, 0, 1, 1, 2, 2]
Explanation:
The array is sorted into ascending order.
Input:
arr[] = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]
Output:
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2]
Explanation:
The array is sorted into ascending order.
1 <= arr.size() <= 10^6
0 <= arr[i] <= 2
-
Dutch National Flag Algorithm:
- This problem can be solved using a two-pointer approach, also known as the Dutch National Flag Algorithm.
- It divides the array into three sections:
- All
0s
will be placed at the beginning. - All
2s
will be placed at the end. - All
1s
will remain in the middle.
- All
- By iterating through the array and performing swaps, we can sort the array in a single pass.
-
Steps:
- Initialize three pointers:
low
,mid
, andhigh
. - Use
low
to track the position of0s
,high
to track the position of2s
, andmid
to traverse the array. - Swap elements as necessary to place
0s
,1s
, and2s
in their respective positions. - Continue until
mid
crosseshigh
.
- Initialize three pointers:
- Expected Time Complexity: O(n), as we iterate through the array exactly once, performing constant-time operations during each step.
- Expected Auxiliary Space Complexity: O(1), as we use only three pointers (
low
,mid
,high
) and no extra data structures.
void sort012(int arr[], int n) {
int low = 0, mid = 0, high = n - 1;
while (mid <= high) {
switch (arr[mid]) {
case 0:
{
int temp = arr[low];
arr[low] = arr[mid];
arr[mid] = temp;
}
low++;
mid++;
break;
case 1:
mid++;
break;
case 2:
{
int temp = arr[high];
arr[high] = arr[mid];
arr[mid] = temp;
}
high--;
break;
}
}
}
class Solution {
public:
void sort012(vector<int>& arr) {
int low = 0, mid = 0, high = arr.size() - 1;
while (mid <= high) {
switch (arr[mid]) {
case 0:
swap(arr[mid++], arr[low++]);
break;
case 1:
mid++;
break;
case 2:
swap(arr[mid], arr[high--]);
break;
}
}
}
};
class Solution {
public void sort012(int[] arr) {
int low = 0, mid = 0, high = arr.length - 1;
while (mid <= high) {
switch (arr[mid]) {
case 0:
int temp0 = arr[low];
arr[low] = arr[mid];
arr[mid] = temp0;
low++;
mid++;
break;
case 1:
mid++;
break;
case 2:
int temp2 = arr[high];
arr[high] = arr[mid];
arr[mid] = temp2;
high--;
break;
}
}
}
}
class Solution:
def sort012(self, arr):
low, mid, high = 0, 0, len(arr) - 1
while mid <= high:
if arr[mid] == 0:
arr[low], arr[mid] = arr[mid], arr[low]
low += 1
mid += 1
elif arr[mid] == 1:
mid += 1
else:
arr[mid], arr[high] = arr[high], arr[mid]
high -= 1
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐