-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountInversions.java
More file actions
55 lines (52 loc) · 1.37 KB
/
CountInversions.java
File metadata and controls
55 lines (52 loc) · 1.37 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
import java.util.*;
public class CountInversions {
public static void main(String[] args) {
long[] arr={2,3,1,4,5};
long N=arr.length;
System.out.println(inversionCount(arr,N));
}
static long inversionCount(long arr[], long N)
{
// Your Code Here
return mergeSort(arr,0,(int)N-1);
}
static long merge(long arr[], int l, int m, int r)
{
// Your code here
int left=l;
int right=m+1;
long count=0;
ArrayList<Long> list=new ArrayList<>();
while(left<=m && right<=r){
if(arr[left]<=arr[right]){
list.add(arr[left]);
left++;
}
else{
list.add(arr[right]);
right++;
count+=(m-left+1);
}
}
while(left<=m){
list.add(arr[left++]);
}
while(right<=r){
list.add(arr[right++]);
}
for(int i=l;i<=r;i++){
arr[i]=list.get(i-l);
}
return count;
}
static long mergeSort(long arr[], int l, int r)
{
long count=0;
if(l>=r) return count;
int mid=(l+r)/2;
count+=mergeSort(arr,l,mid);
count+=mergeSort(arr,mid+1,r);
count+=merge(arr,l,mid,r);
return count;
}
}