-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdutch_national_flag_problem.cpp
43 lines (37 loc) · 1.09 KB
/
dutch_national_flag_problem.cpp
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
//https://ide.geeksforgeeks.org/egew3jtW3j
//dutch national flag problem
#include<bits/stdc++.h>
using namespace std;
int mod=1e9+7;
#define F(a,b,var) for(int var=a;var<b;var++)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
int main()
{
//code
FAST_INP;
int T;
cin>>T;
while(T--)
{
int n, pivot;
cin>>n>>pivot;
vector<int> A(n);
for(int i=0; i<n; i++)cin>>A[i];
//int pivot_ele = A[pivot]; //0 based
int smallest = 0, current = 0, largest = n - 1;
while(current < largest){
if(A[current] < A[pivot]){
swap(A[smallest], A[current]);
smallest++; current ++;
}
else if(A[current] == A[pivot])
current++;
else{
swap(A[current], A[largest]);
current++; largest--;
}
}
for(int i=0; i<n; i++)cout<<A[i]<<" ";
}
return 0;
}