forked from jugshaurya/recursion-CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countInversion.cpp
68 lines (53 loc) · 1.48 KB
/
countInversion.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Q-->Let A[0 ... n-1] be an array of n distinct positive integers. If i < j and A[i] > A[j]
then the pair (i, j) is called an inversion of A (where i and j are indexes of A).
Given an integer array A, your task is to find the number of inversions in A.
//constraints
1 <= n <= 10^5
1 <= A[i] <= 10^9
*/
#include <iostream>
using namespace std;
long long countDuringMerging(int* arr, int start, int end){
int newarray[1000000];
int mid = (start+end)/2;
int i=start;
int j=mid+1;
int k=start;
long long inversionCount = 0 ;
while(i<=mid && j<=end){
if(arr[i] <= arr[j]){
newarray[k++] = arr[i++];
}else{
inversionCount += (mid+1) - i; // (total length) - i
newarray[k++] = arr[j++];
}
}
while(i<=mid){
newarray[k++] =arr[i++];
}
while(j<=end){
newarray[k++] = arr[j++];
}
for(int m = start; m<k; m++){
arr[m] = newarray[m];
}
return inversionCount;
}
long long inversionCount(int* arr, int s, int e){
if(s>=e){
return 0;
}
int mid = (s+e)/2;
long long leftInversionCount = inversionCount(arr,s,mid);
long long rightInversionCount = inversionCount(arr,mid+1,e);
return leftInversionCount + rightInversionCount + countDuringMerging(arr,s,e);
}
long long solve(int A[], int n)
{
return inversionCount(A,0,n-1);
}
int main(){
int a[] = {3,2,1};
cout<<solve(a,5);//ans-->3
}