-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDutch_National_Flag_Problem.cpp
More file actions
55 lines (45 loc) · 1.14 KB
/
Dutch_National_Flag_Problem.cpp
File metadata and controls
55 lines (45 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Problem Statement-Given an array containing 0s, 1s and 2s, sort the array in-place. You should treat numbers of the array as objects, hence, we can’t count 0s, 1s, and 2s to recreate the array.
The flag of the Netherlands consists of three colors: red, white and blue; and since our input array also consists of three different numbers that is why it is called Dutch National Flag problem.
Examples
Input: [1, 0, 2, 1, 0]
Output: [0 0 1 1 2]
Input: [2, 2, 0, 1, 2, 0]
Output: [0 0 1 2 2 2 ]
*/
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
vector<int> solution(vector<int>&arr)
{
int low=0,high=arr.size()-1;
for(int i=0;i<=high;)
{
if(arr[i]==0)
{
swap(arr[i],arr[low]);
i++;
low++;
}
else if(arr[i]==1)
{
i++;
}
else
{
swap(arr[i],arr[high]);
high--;
}
}
return arr;
}
int main()
{
vector<int> arr={0,1,0,2,1,2};
vector<int> result=solution(arr);
for(int i=0;i<result.size();i++)
{
cout<<result[i]<<" ";
}
return 0;
}