Maximum inversions in a sequence of 1 to N after performing given operations at most K times
Last Updated :
31 Jan, 2022
Given two integers N and K, the task is to find the maximum number of inversion in a sequence of first N natural numbers after performing at max K operations. In each operation, any two elements of the sequence can be swapped. Note: elements of the sequence are arranged in ascending order and there are no repeated elements in the sequence.
Example:
Input: N = 5, K = 3
Output: 10
Explanation:
Initially we have the sequence as {1, 2, 3, 4, 5}.
In the first operation, we can swap 1 and 5, so sequence becomes {5, 2, 3, 4, 1}.
In the second operation, we can swap 2 and 4, so sequence becomes {5, 4, 3, 2, 1}.
We don’t require any more operations. Thus, the number of inversions in the above sequence is 10
Input: N = 4, K = 1
Output: 5
Explanation:
Initially we have the sequence as { 1, 2, 3, 4}.
In the first operation we can swap 1 and 4, so the sequence becomes {4, 2, 3, 1 }.
Since we can perform only 1 operation, so this is our final sequence. Thus, the number of inversions in the above sequence is 5.
Approach:
- Since elements arranged in ascending order is perfect i.e, it has 0 inversion and elements arranged in descending order is least perfect i.e., it has maximum inversion.
- So, the idea is to make the sequence more closer to descending order with each swap operation in order to get maximum inversions. Thus, in first operation, we need to swap the largest and the smallest element. Similarly, in i-th operation, we need to swap i-th largest element with i-th smallest element of the sequence.
- The number of swaps required to convert the sequence in ascending order into descending order is N/2. Thus, the number of operations performed should be less than or equal to N/2.
So update K as:
K = min ( K, N / 2 )
- We need to maintain two variables ‘left’ and ‘right’ to represent i-th minimum and i-th maximum element of the sequence respectively.
- Initialize ‘left’ to 1 and ‘right’ to N.
- We need to perform following operations until K becomes 0:
- On, swapping ‘left’ and ‘right’ in the sequence number of inversion increases by
2 * ( left – right ) – 1
- So, add this value to the answer.
- This is because when we swap ‘left’ and ‘right’, all pairs of the form (right, i) contributes to the inversions and also all pairs of the form (i, left) contributes to the inversions, where left < i < right.
- Total number of pairs of the form (right, i) => right – left, where left <= i < right
- Total number of pairs of the form (i, left) => right – left, where left< i <=right
- Total number of such pairs is 2 * ( right – left )
- We subtract 1 from it, because the pair (right, left) was counted twice in the above computation.
- Decrease value of K and ‘right’ by 1 and increase ‘left’ by 1.
- Print the value of answer
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximum_inversion( int n, int k)
{
int answer = 0;
k = min(k, n / 2);
int left = 1;
int right = n;
while (k--) {
answer += 2 * (right - left) - 1;
left++;
right--;
}
cout << answer << endl;
}
int main()
{
int N = 5;
int K = 3;
maximum_inversion(N, K);
N = 4;
K = 1;
maximum_inversion(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maximum_inversion( int n, int k)
{
int answer = 0 ;
k = Math.min(k, n / 2 );
int left = 1 ;
int right = n;
while (k != 0 )
{
k--;
answer += 2 * (right - left) - 1 ;
left++;
right--;
}
System.out.println(answer);
}
public static void main(String s[])
{
int N = 5 ;
int K = 3 ;
maximum_inversion(N, K);
N = 4 ;
K = 1 ;
maximum_inversion(N, K);
}
}
|
Python3
def maximum_inversion(n, k):
answer = 0 ;
k = min (k, n / / 2 );
left = 1 ;
right = n;
while (k > 0 ):
k - = 1 ;
answer + = 2 * (right - left) - 1 ;
left + = 1 ;
right - = 1 ;
print (answer);
if __name__ = = '__main__' :
N = 5 ;
K = 3 ;
maximum_inversion(N, K);
N = 4 ;
K = 1 ;
maximum_inversion(N, K);
|
C#
using System;
class GFG{
static void maximum_inversion( int n, int k)
{
int answer = 0;
k = Math.Min(k, n / 2);
int left = 1;
int right = n;
while (k != 0)
{
k--;
answer += 2 * (right - left) - 1;
left++;
right--;
}
Console.WriteLine(answer);
}
public static void Main(String []s)
{
int N = 5;
int K = 3;
maximum_inversion(N, K);
N = 4;
K = 1;
maximum_inversion(N, K);
}
}
|
Javascript
<script>
function maximum_inversion(n, k) {
var answer = 0;
k = Math.min(k, parseInt(n / 2));
var left = 1;
var right = n;
while (k > 0)
{
k--;
answer += 2 * (right - left) - 1;
left++;
right--;
}
document.write(answer + "<br/>" );
}
var N = 5;
var K = 3;
maximum_inversion(N, K);
N = 4;
K = 1;
maximum_inversion(N, K);
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...