forked from imsushant12/GFG-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count_Inversions.cpp
100 lines (79 loc) · 2.54 KB
/
Count_Inversions.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
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
/*
Problem Statement:
-----------------
Given an array of integers. Find the Inversion Count in the array.
Inversion Count: For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then the inversion count is 0. If an array is sorted in the reverse order then the inversion count is the maximum.
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Example 1:
---------
Input: N = 5, arr[] = {2, 4, 1, 3, 5}
Output: 3
Explanation: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
Example 2:
---------
Input: N = 5
arr[] = {2, 3, 4, 5, 6}
Output: 0
Explanation: As the sequence is already sorted so there is no inversion count.
Example 3:
---------
Input: N = 3, arr[] = {10, 10, 10}
Output: 0
Explanation: As all the elements of array are same, so there is no inversion count.
Your Task: You don't need to read input or print anything. Your task is to complete the function inversionCount() which takes the array arr[]
and the size of the array as inputs and returns the inversion count of the given array.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
*/
// Link --> https://practice.geeksforgeeks.org/problems/inversion-of-array-1587115620/1#
// Code:
class Solution
{
public:
long long int count = 0;
void merge(long long a[] , long long p , long long q , long long r)
{
long long l = q-p+1;
long long a1[l];
long long l2 = r-q;
long long a2[l2];
for(long long i=0 ; i<l ; i++)
a1[i] = a[i+p];
for(long long i=0 ; i<l2 ; i++)
a2[i] = a[q+i+1];
long long left=0 , right=0 , k=p;
while(left < l && right < l2)
{
if(a1[left] <= a2[right])
{
a[k] = a1[left];
left++;
}
else
{
a[k] = a2[right];
right++;
count += (l-left);
}
k++;
}
while(left < l)
a[k++] = a1[left++];
}
void mergeLogic(long long a[] , long long l , long long r)
{
if(l >= r)
{
return;
}
int mid = l + (r-l)/2;
mergeLogic(a,l,mid);
mergeLogic(a,mid+1,r);
merge(a,l,mid,r);
}
long long int inversionCount(long long a[] , long long n)
{
mergeLogic(a , 0 , n-1);
return count;
}
};