-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCountInversion.java
More file actions
166 lines (116 loc) * 3.76 KB
/
CountInversion.java
File metadata and controls
166 lines (116 loc) * 3.76 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// M - I
class Solution {
static long count;
static long inversionCount(long arr[], long N){
count = 0L;
long temp[] = new long[(int)N];
mergeSort(arr, 0, N-1, temp);
return count;
}
public static void mergeTwoArrays(long arr[], long low, long mid, long high, long merged[]) {
// 1st array low - mid
// 2nd array mid+1 - high
long i=low, j=mid+1, vidx = low;
while(i<=mid && j<=high) {
if(arr[(int)i] <= arr[(int)j]) {
merged[(int)vidx++] = arr[(int)i++];
} else {
count += mid + 1 - i;
merged[(int)vidx++] = arr[(int)j++];
}
}
while(i<=mid) {
merged[(int)vidx++] = arr[(int)i++];
}
while(j<=high) {
merged[(int)vidx++] = arr[(int)j++];
}
for(long val=low; val<=high; val++) {
arr[(int)val] = merged[(int)val];
}
}
public static void mergeSort(long arr[], long low, long high, long temp[]) {
if(low < high) {
long mid = (low+high) >> 1;
mergeSort(arr, low, mid, temp);
mergeSort(arr, mid+1, high, temp);
mergeTwoArrays(arr, low, mid, high, temp);
// 1st array low - mid
// 2nd array mid+1 - high
}
}
}
// M - II
private static int mergeCount(int[] arr, int l, int m, int r) {
int[] left = Arrays.copyOfRange(arr, l, m + 1);
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l, swap = 0;
while (i < left.length && j < right.length)
{
if (left[i] <= right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swap += (m + 1) - (l + i);
}
}
return swap;
}
private static int mergeSort(int[] arr, int l, int r) {
int count = 0;
if (l < r) {
int m = (l + r) / 2;
count += mergeSort(arr, l, m);
count += mergeSort(arr, m + 1, r);
count += mergeCount(arr, l, m, r);
}
return count;
}
// M-II
import java.util.*;
import java.io.*;
public class Main {
static int count;
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
}
count = 0;
mergeSort(arr, 0, n-1);
System.out.println(count);
}
public static int[] mergeTwoArrays(int left[], int right[]) {
int merged[] = new int[left.length + right.length];
int i=0, j=0, vidx = 0;
while(i<left.length && j<right.length) {
if(left[i] <= right[j]) {
merged[vidx++] = left[i++];
} else {
count += left.length - i;
merged[vidx++] = right[j++];
}
}
while(i<left.length) {
merged[vidx++] = left[i++];
}
while(j<right.length) {
merged[vidx++] = right[j++];
}
return merged;
}
public static int[] mergeSort(int arr[], int low, int high) {
if(low == high) {
int temp[] = new int[1];
temp[0] = arr[low];
return temp;
}
int mid = (low+high)/2;
int left[] = mergeSort(arr, low, mid);
int right[] = mergeSort(arr, mid+1, high);
int merged[] = mergeTwoArrays(left, right);
return merged;
}
}