Skip to content

Commit 1ef03d5

Browse files
committed
count inv
1 parent 737850e commit 1ef03d5

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

DSA Crack Sheet/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/ "view questions") - [Cpp Solution](./solutions/Find%20the%20Duplicate%20Number.cpp)
1717
- [Merge Intervals](https://leetcode.com/problems/merge-intervals/ "view questions") - [Cpp Solution](./solutions/Merge%20Intervals.cpp)
1818
- [Next Permutation](https://leetcode.com/problems/next-permutation/ "view questions") - [Cpp Solution](./solutions/Next%20Permutation.cpp)
19+
- [Count Inversions](https://practice.geeksforgeeks.org/problems/inversion-of-array-1587115620/1# "view questions") - [Cpp Solution](./solutions/Count%20Inversions.cpp)
1920
- []( "view questions") - [Cpp Solution](./solutions/.cpp)
2021

2122
<!-- - []( "view questions") - [Cpp Solution](./solutions/) -->
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Count Inversions
3+
================
4+
5+
Given an array of integers. Find the Inversion Count in the array.
6+
7+
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.
8+
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
9+
10+
Example 1:
11+
Input: N = 5, arr[] = {2, 4, 1, 3, 5}
12+
Output: 3
13+
Explanation: The sequence 2, 4, 1, 3, 5
14+
has three inversions (2, 1), (4, 1), (4, 3).
15+
16+
Example 2:
17+
Input: N = 5
18+
arr[] = {2, 3, 4, 5, 6}
19+
Output: 0
20+
Explanation: As the sequence is already
21+
sorted so there is no inversion count.
22+
23+
Example 3:
24+
Input: N = 3, arr[] = {10, 10, 10}
25+
Output: 0
26+
Explanation: As all the elements of array
27+
are same, so there is no inversion count.
28+
29+
Your Task:
30+
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.
31+
32+
Expected Time Complexity: O(nLogn).
33+
Expected Auxiliary Space: O(n).
34+
35+
Constraints:
36+
1 ≤ N ≤ 5*105
37+
1 ≤ C ≤ 1018
38+
*/
39+
40+
long long merge(long long arr[], long long temp[], int left, int mid, int right)
41+
{
42+
int i, j, k;
43+
long long inv_count = 0;
44+
45+
i = left, j = mid, k = left;
46+
while ((i <= mid - 1) && (j <= right))
47+
{
48+
if (arr[i] <= arr[j])
49+
temp[k++] = arr[i++];
50+
else
51+
{
52+
temp[k++] = arr[j++];
53+
inv_count = inv_count + (mid - i);
54+
}
55+
}
56+
57+
while (i <= mid - 1)
58+
temp[k++] = arr[i++];
59+
while (j <= right)
60+
temp[k++] = arr[j++];
61+
62+
for (i = left; i <= right; i++)
63+
arr[i] = temp[i];
64+
return inv_count;
65+
}
66+
67+
long long _mergeSort(long long arr[], long long temp[], int left, int right)
68+
{
69+
long long mid, inv_count = 0;
70+
if (right > left)
71+
{
72+
mid = (right + left) / 2;
73+
74+
inv_count += _mergeSort(arr, temp, left, mid);
75+
inv_count += _mergeSort(arr, temp, mid + 1, right);
76+
inv_count += merge(arr, temp, left, mid + 1, right);
77+
}
78+
return inv_count;
79+
}
80+
81+
// Function to find inversion count in the array
82+
// arr[]: Input Array
83+
// N : Size of the Array arr[]
84+
85+
long long int inversionCount(long long arr[], long long N)
86+
{
87+
long long temp[N];
88+
return _mergeSort(arr, temp, 0, N - 1);
89+
}

0 commit comments

Comments
 (0)