Count inversions in a sequence generated by appending given array K times
Given an array arr[], the task is to append the given array exactly K – 1 times to its end and print the total number of inversions in the resulting array.
Examples:
Input: arr[]= {2, 1, 3}, K = 3
Output: 12
Explanation:
Appending 2 copies of array arr[] modifies arr[] to {2, 1, 3, 2, 1, 3, 2, 1, 3}
The pairs (arr[i], arr[j]), where i < j and arr[i] > arr[j] are (2, 1), (2, 1), (2, 1), (3, 2), (3, 1), (3, 2), (3, 1), (2, 1), (2, 1), (3, 2), (3, 1), (2, 1)
Therefore, the total number of inversions are 12.
Input: arr[]= {6, 2}, K = 2
Output: 3
Explanation:
Appending 2 copies of array arr[] = {6, 2, 6, 2}
The pairs (arr[i], arr[j]), where i < j and arr[i] > arr[j] are (6, 2), (6, 2), (6, 2)
Therefore, the total number of inversions are 3.
Naive Approach: The simplest approach is to store K copies of the given array in a vector and then, find the count of inversions of the resulting vector.
Time Complexity: O(N2)
Auxiliary Space: O(K * N)
Efficient Approach: The idea to solve this problem is to first find the total number of inversions in the given array, say inv. Then, count pairs of distinct elements in a single copy, say X. Now, calculate the total number of inversions after appending K copies of the array by the equation:
(inv*K + ((K*(K-1))/2)*X).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void totalInversions( int arr[],
int K, int N)
{
int inv = 0;
int X = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (arr[i] > arr[j] and i < j)
inv++;
if (arr[i] > arr[j])
X++;
}
}
int totalInv = X * K * (K - 1) / 2
+ inv * K;
cout << totalInv << endl;
}
int main()
{
int arr[] = { 2, 1, 3 };
int K = 3;
int N = sizeof (arr) / sizeof (arr[0]);
totalInversions(arr, K, N);
}
|
Java
import java.util.*;
class GFG
{
static void totalInversions( int arr[],
int K, int N)
{
int inv = 0 ;
int X = 0 ;
int i, j;
for (i = 0 ; i < N; i++)
{
for (j = 0 ; j < N; j++)
{
if (arr[i] > arr[j] && i < j)
inv++;
if (arr[i] > arr[j])
X++;
}
}
int totalInv = X * K * (K - 1 ) / 2
+ inv * K;
System.out.println(totalInv);
}
public static void main(String args[])
{
int arr[] = { 2 , 1 , 3 };
int K = 3 ;
int N = arr.length;
totalInversions(arr, K, N);
}
}
|
Python3
def totalInversions(arr, K, N) :
inv = 0
X = 0
for i in range (N):
for j in range (N):
if (arr[i] > arr[j] and i < j) :
inv + = 1
if (arr[i] > arr[j]) :
X + = 1
totalInv = X * K * (K - 1 ) / / 2 + inv * K
print (totalInv)
arr = [ 2 , 1 , 3 ]
K = 3
N = len (arr)
totalInversions(arr, K, N)
|
C#
using System;
class GFG
{
static void totalInversions( int []arr,
int K, int N)
{
int inv = 0;
int X = 0;
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (arr[i] > arr[j] && i < j)
inv++;
if (arr[i] > arr[j])
X++;
}
}
int totalInv = X * K * (K - 1) / 2
+ inv * K;
Console.WriteLine(totalInv);
}
public static void Main()
{
int []arr = { 2, 1, 3 };
int K = 3;
int N = arr.Length;
totalInversions(arr, K, N);
}
}
|
Javascript
<script>
function totalInversions(arr, K, N)
{
let inv = 0;
let X = 0;
let i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (arr[i] > arr[j] && i < j)
inv++;
if (arr[i] > arr[j])
X++;
}
}
let totalInv = X * K * (K - 1) / 2
+ inv * K;
document.write(totalInv);
}
let arr = [ 2, 1, 3 ];
let K = 3;
let N = arr.length;
totalInversions(arr, K, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
07 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...