-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew Year Chaos.cpp
33 lines (28 loc) · 921 Bytes
/
New Year Chaos.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
/*
Problem Title: New Year Chaos
Problem URL: https://www.hackerrank.com/challenges/one-month-preparation-kit-new-year-chaos/problem?h_l=interview&playlist_slugs%5B%5D=preparation-kits&playlist_slugs%5B%5D=one-month-preparation-kit&playlist_slugs%5B%5D=one-month-week-three
Max Score: 100
Score: 100
Language: C++
Category: One Month Preparation
*/
void minimumBribes(vector<int> q) {
int bribes = 0, n = q.size();
for (int i = n-1; i > -1; --i)
if (q[i] != (i+1)) {
if (i!=0 && q[i-1] == (i+1)) {
swap(q[i-1], q[i]);
bribes++;
}
else if (i>1 && q[i-2] == (i+1)) {
swap(q[i-2], q[i-1]);
swap(q[i-1], q[i]);
bribes+=2;
}
else {
cout << "Too chaotic\n";
return;
}
}
cout << bribes << '\n';
}