-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.c
More file actions
51 lines (44 loc) · 1.22 KB
/
Copy pathpattern.c
File metadata and controls
51 lines (44 loc) · 1.22 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
#include <bits/stdc++.h>
using namespace std;
// Step 1: Inorder traversal of array representation of CBT
void inorder(vector<int>& arr, vector<int>& in, int n, int index) {
if (index >= n) return;
inorder(arr, in, n, 2 * index + 1);
in.push_back(arr[index]);
inorder(arr, in, n, 2 * index + 2);
}
// Step 2: Count min swaps to sort
int minSwapsToSort(vector<int>& in) {
int n = in.size();
vector<pair<int,int>> v(n);
for (int i = 0; i < n; i++) v[i] = {in[i], i};
sort(v.begin(), v.end());
vector<bool> visited(n, false);
int swaps = 0;
for (int i = 0; i < n; i++) {
// Already visited or in correct place
if (visited[i] || v[i].second == i)
continue;
int cycleSize = 0;
int j = i;
while (!visited[j]) {
visited[j] = true;
j = v[j].second;
cycleSize++;
}
if (cycleSize > 1)
swaps += (cycleSize - 1);
}
return swaps;
}
// Step 3: Main function
int minSwaps(vector<int>& arr) {
vector<int> in;
inorder(arr, in, arr.size(), 0);
return minSwapsToSort(in);
}
int main() {
vector<int> arr = {5, 6, 7, 8, 9, 10, 11};
cout << minSwaps(arr);
return 0;
}