Count inversions in a sequence generated by appending given array K times
Last Updated :
07 Apr, 2022
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of
// inversions in K copies of given array
void totalInversions(int arr[],
int K, int N)
{
// Stores count of inversions
// in the given array
int inv = 0;
// Stores the count of pairs
// of distinct array elements
int X = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Generate each pair
for (int j = 0; j < N; j++) {
// Check for each pair, if the
// condition is satisfied or not
if (arr[i] > arr[j] and i < j)
inv++;
// If pairs consist of
// distinct elements
if (arr[i] > arr[j])
X++;
}
}
// Count inversion in the sequence
int totalInv = X * K * (K - 1) / 2
+ inv * K;
// Print the answer
cout << totalInv << endl;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 1, 3 };
// Given K
int K = 3;
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
totalInversions(arr, K, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to count the number of
// inversions in K copies of given array
static void totalInversions(int arr[],
int K, int N)
{
// Stores count of inversions
// in the given array
int inv = 0;
// Stores the count of pairs
// of distinct array elements
int X = 0;
int i, j;
// Traverse the array
for (i = 0; i < N; i++)
{
// Generate each pair
for (j = 0; j < N; j++)
{
// Check for each pair, if the
// condition is satisfied or not
if(arr[i] > arr[j] && i < j)
inv++;
// If pairs consist of
// distinct elements
if(arr[i] > arr[j])
X++;
}
}
// Count inversion in the sequence
int totalInv = X * K * (K - 1) / 2
+ inv * K;
// Print the answer
System.out.println(totalInv);
}
// Driver Code
public static void main(String args[])
{
// Given array
int arr[] = { 2, 1, 3 };
// Given K
int K = 3;
// Size of the array
int N = arr.length;
totalInversions(arr, K, N);
}
}
// This code is contributed by bgangwar59.
Python3
# Python program of the above approach
# Function to count the number of
# inversions in K copies of given array
def totalInversions(arr, K, N) :
# Stores count of inversions
# in the given array
inv = 0
# Stores the count of pairs
# of distinct array elements
X = 0
# Traverse the array
for i in range(N):
# Generate each pair
for j in range(N):
# Check for each pair, if the
# condition is satisfied or not
if (arr[i] > arr[j] and i < j) :
inv += 1
# If pairs consist of
# distinct elements
if (arr[i] > arr[j]) :
X += 1
# Count inversion in the sequence
totalInv = X * K * (K - 1) // 2 + inv * K
# Print the answer
print(totalInv)
# Driver Code
# Given array
arr = [ 2, 1, 3 ]
# Given K
K = 3
# Size of the array
N = len(arr)
totalInversions(arr, K, N)
# This code is contributed by susmitakundugoaldanga
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to count the number of
// inversions in K copies of given array
static void totalInversions(int []arr,
int K, int N)
{
// Stores count of inversions
// in the given array
int inv = 0;
// Stores the count of pairs
// of distinct array elements
int X = 0;
int i, j;
// Traverse the array
for (i = 0; i < N; i++)
{
// Generate each pair
for (j = 0; j < N; j++)
{
// Check for each pair, if the
// condition is satisfied or not
if(arr[i] > arr[j] && i < j)
inv++;
// If pairs consist of
// distinct elements
if(arr[i] > arr[j])
X++;
}
}
// Count inversion in the sequence
int totalInv = X * K * (K - 1) / 2
+ inv * K;
// Print the answer
Console.WriteLine(totalInv);
}
// Driver Code
public static void Main()
{
// Given array
int []arr = { 2, 1, 3 };
// Given K
int K = 3;
// Size of the array
int N = arr.Length;
totalInversions(arr, K, N);
}
}
// This code is contributed by jana_sayantan.
JavaScript
<script>
// JavaScript program for
// the above approach
// Function to count the number of
// inversions in K copies of given array
function totalInversions(arr, K, N)
{
// Stores count of inversions
// in the given array
let inv = 0;
// Stores the count of pairs
// of distinct array elements
let X = 0;
let i, j;
// Traverse the array
for (i = 0; i < N; i++)
{
// Generate each pair
for (j = 0; j < N; j++)
{
// Check for each pair, if the
// condition is satisfied or not
if(arr[i] > arr[j] && i < j)
inv++;
// If pairs consist of
// distinct elements
if(arr[i] > arr[j])
X++;
}
}
// Count inversion in the sequence
let totalInv = X * K * (K - 1) / 2
+ inv * K;
// Print the answer
document.write(totalInv);
}
// Driver Code
// Given array
let arr = [ 2, 1, 3 ];
// Given K
let K = 3;
// Size of the array
let N = arr.length;
totalInversions(arr, K, N);
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Count inversions of size k in a given array Given an array of n distinct integers a_{1}, a_{2}, ..., a_{n} and an integer k. Find out the number of sub-sequences of a such that a_{i_{1}} > a_{i_{2}} > ... > a_{i_{k}} , and 1 <= i_{1} < i_{2} < ... < i_{k} <= n . In other words output the total number of inversions of l
14 min read
Count subsequences 01 in string generated by concatenation of given numeric string K times Given a string S and a positive integer K, the task is to find the number of subsequences "01" in the string generated by concatenation of the given numeric string S K times. Examples: Input: S = "0171", K = 2Output: 6Explanation:The string formed by concatenation of S, K number of times is "0171017
6 min read
Count of Ks in the Array for a given range of indices after array updates for Q queries Given an array arr[] of N integers, an integer K, and Q queries of the type as explained below: (1, L, R): If the query is of type 1, then find the number of Ks over the range [L, R].(2, P, X): If the query is of type 2, then update the array element at index P to X. The task is to perform the queri
15+ min read
Count all elements in the array which appears at least K times after their first occurrence Given an array arr[] of N integer elements and an integer K. The task is to count all distinct arr[i] such that arr[i] appears at least K times in the index range i + 1 to n - 1.Examples: Input: arr[] = {1, 2, 1, 3}, K = 1 Output: 1 arr[0] = 1 is the only element that appears at least once in the in
15 min read
Number of subsequences as "ab" in a string repeated K times Given a String S, consider a new string formed by repeating the S exactly K times. We need find the number of subsequences as âabâ in the newly formed string. Examples : Input : S = "abcb" K = 2 Output : 6 Here, Given string is repeated 2 times and we get a new string "abcbabcb" Below are 6 occurren
10 min read