Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added count inversions in cpp #259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Array/Count Inversions/Count Inversions - Question.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Count Inversions:
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).
88 changes: 88 additions & 0 deletions Array/Count Inversions/count-inversions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <bits/stdc++.h>
using namespace std;


// } Driver Code Ends
class Solution{
public:
// arr[]: Input Array
// N : Size of the Array arr[]
// Function to count inversions in the array.
long long merge(long long arr[], long long temp[], long long left, long long mid, long long right){
long long i, j, k;
long long inv_count = 0;

i = left;
j = mid;
k = left;

while(( i <= mid - 1)&&( j <= right )){
if(arr[i] <= arr[j]){
temp[k++] = arr[i++];
}
else{
temp[k++] = arr[j++];

inv_count = inv_count + (mid - i);
}
}

while( i <= mid - 1)
temp[k++] = arr[i++];
while( j <= right )
temp[k++] = arr[j++];

for(i = left; i <= right; i++ ){
arr[i] = temp[i];
}

return inv_count;
}

long long mergesort( long long arr[], long long temp[], long long left, long long right){
long long mid, inv_count = 0;

if(right > left){

mid = (left + right)/2;

inv_count += mergesort(arr, temp, left, mid);
inv_count += mergesort(arr, temp, mid + 1, right);

inv_count += merge(arr, temp, left, mid + 1, right);
}

return inv_count;
}

long long inversionCount(long long arr[], long long N)
{
long long temp[N];
long long ct = mergesort(arr, temp, 0, N - 1);
return ct;
}

};

// { Driver Code Starts.

int main() {

long long T;
cin >> T;

while(T--){
long long N;
cin >> N;

long long A[N];
for(long long i = 0;i<N;i++){
cin >> A[i];
}
Solution obj;
cout << obj.inversionCount(A,N) << endl;
}

return 0;
}
// } Driver Code Ends