-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountInversions.java
More file actions
96 lines (75 loc) · 2.72 KB
/
CountInversions.java
File metadata and controls
96 lines (75 loc) · 2.72 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
package Problem12;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CountInversions {
int seg[];
// Constructor to initialize segment tree
public CountInversions(int n) {
seg = new int[4 * n];
}
// Update the Segment Tree
public void update(int ind, int low, int high, int i, int val) {
if (low == high) {
seg[ind] += val;
return;
}
int mid = (low + high) / 2;
if (i <= mid) {
update(2 * ind + 1, low, mid, i, val); // Update left child
} else {
update(2 * ind + 2, mid + 1, high, i, val); // Update right child
}
seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2]; // Merge results
}
// Query to find the sum in a given range
public int query(int ind, int low, int high, int l, int r) {
if (r < low || l > high) {
return 0; // Out of range
}
if (l <= low && high <= r) {
return seg[ind]; // Completely within range
}
int mid = (low + high) / 2;
int left = query(2 * ind + 1, low, mid, l, r); // Query left subtree
int right = query(2 * ind + 2, mid + 1, high, l, r); // Query right subtree
return left + right;
}
// Count inversions in the array
public int countInversions(int[] arr) {
// Step 1: Coordinate Compression
int n = arr.length;
int[] sortedArr = arr.clone();
Arrays.sort(sortedArr);
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(sortedArr[i], i + 1); // Map original values to compressed values
}
// Step 2: Process the array in reverse order
int inversions = 0;
for (int i = n - 1; i >= 0; i--) {
int compressedValue = map.get(arr[i]);
// Count elements less than the current element
inversions += query(0, 0, n - 1, 0, compressedValue - 1);
// Insert the current element into the Segment Tree
update(0, 0, n - 1, compressedValue, 1);
}
return inversions;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Initialize Segment Tree
CountInversions ci = new CountInversions(n);
// Count inversions
int result = ci.countInversions(arr);
System.out.println("Number of inversions: " + result);
}
}