-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path6_Question.cpp
More file actions
60 lines (42 loc) · 1.51 KB
/
6_Question.cpp
File metadata and controls
60 lines (42 loc) · 1.51 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
56
57
58
59
60
// Airport security officials have confiscated several item of the passengers at the security check point.
// All the items have been dumped into a huge box (array). Each item possesses a certain amount of risk[0,1,2].
// Here, the risk severity of the items represent an array[] of N number of integer values.
// The task here is to sort the items based on their levels of risk in the array. The risk values range from 0 to 2.
// Example :
// Input :
// 7 -> Value of N
// [1,0,2,0,1,0,2]-> Element of arr[0] to arr[N-1], while input each element is separated by new line.
// Output :
// 0 0 0 1 1 2 2 -> Element after sorting based on risk severity
// Example 2:
// input : 10 -> Value of N
// [2,1,0,2,1,0,0,1,2,0] -> Element of arr[0] to arr[N-1], while input each element is separated by a new line.
// Output :
// 0 0 0 0 1 1 1 2 2 2 ->Elements after sorting based on risk severity.
// Explanation:
// In the above example, the input is an array of size N consisting of only 0’s, 1’s and 2s.
// The output is a sorted array from 0 to 2 based on risk severity.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> arr(n);
for(int i=0; i<n; i++){
cin>>arr[i];
}
int l = 0, m = 0, h = n-1;
while(m <= h){
if(arr[m] == 0){
swap(arr[l++],arr[m++]);
}else if(arr[m] == 1){
m++;
}else{
swap(arr[m],arr[h--]);
}
}
for(int x : arr){
cout<<x<<" ";
}
return 0;
}