-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathinversionCount.cpp
More file actions
55 lines (55 loc) · 1.13 KB
/
inversionCount.cpp
File metadata and controls
55 lines (55 loc) · 1.13 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
#include<bits/stdc++.h>
using namespace std;
long long helper(int A[], int start, int end)
{
if(start >= end)
return 0;
int mid = (end + start) / 2;
long long count = 0;
count += helper(A, start, mid);
count += helper(A, mid + 1, end);
int i = start, j = mid + 1, k = 0;
int copy[end - start + 1];
while(i <= mid && j <= end)
{
if(A[j] < A[i])
{
count += end - i + 1;
copy[k] = A[j];
j++;
}
else if(A[j] > A[i])
{
copy[k] = A[i];
i++;
}
k++;
}
while(i <= mid)
{
copy[k] = A[i];
i++;
k++;
}
while(j <= end)
{
copy[k] = A[j];
j++;
k++;
}
for(int l = 0; l <= (end - start); l++)
A[start + i] = copy[i];
return count;
}
long long solve(int A[], int n)
{
return helper(A, 0, n - 1);
}
int main(int argc, char const *argv[])
{
int A[] = {2, 5, 1, 3, 4};
solve(A, 5);
for(int i = 0; i < 5; i++)
cout<<A[i]<<" ";
return 0;
}