Maximum inversions in a sequence of 1 to N after performing given operations at most K times

• Last Updated : 21 Apr, 2021

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:
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:

1. 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.
2. 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.
3. 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 )

1. We need to maintain two variables ‘left’ and ‘right’ to represent i-th minimum and i-th maximum element of the sequence respectively.
2. Initialize ‘left’ to 1 and ‘right’ to N.
3. 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

• 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.
1. Print the value of answer

Below is the implementation of the above approach:

C++

 // C++ program for the above approach #include using namespace std; // Function which computes the// maximum number of inversionsint maximum_inversion(int n, int k){    // 'answer' will store the required    // number of inversions    int answer = 0;     // We do this because we will    // never require more than    // floor(n/2) operations    k = min(k, n / 2);     // left pointer in the array    int left = 1;    // right pointer in the array    int right = n;     // Doing k operations    while (k--) {        // Incrementing ans by number        // of inversions increase due        // to this swapping        answer += 2 * (right - left) - 1;        left++;        right--;    }    cout << answer << endl;} // Driver codeint main(){    // Input 1    int N = 5;    int K = 3;    maximum_inversion(N, K);     // Input 2    N = 4;    K = 1;    maximum_inversion(N, K);     return 0;}

Java

 // Java program for the above approachimport java.util.*; class GFG{     // Function which computes the// maximum number of inversionsstatic void maximum_inversion(int n, int k){         // 'answer' will store the required    // number of inversions    int answer = 0;     // We do this because we will    // never require more than    // floor(n/2) operations    k = Math.min(k, n / 2);     // left pointer in the array    int left = 1;         // right pointer in the array    int right = n;     // Doing k operations    while (k != 0)    {        k--;                 // Incrementing ans by number        // of inversions increase due        // to this swapping        answer += 2 * (right - left) - 1;        left++;        right--;    }    System.out.println(answer);} // Driver Codepublic static void main(String s[]){         // Input 1    int N = 5;    int K = 3;    maximum_inversion(N, K);         // Input 2    N = 4;    K = 1;    maximum_inversion(N, K);}} // This code is contributed by rutvik_56

Python3

 # Python3 program for the above approach # Function which computes the# maximum number of inversionsdef maximum_inversion(n, k):         # 'answer' will store the required    # number of inversions    answer = 0;     # We do this because we will    # never require more than    # floor(n/2) operations    k = min(k, n // 2);     # left pointer in the array    left = 1;     # right pointer in the array    right = n;     # Doing k operations    while (k > 0):        k -= 1;         # Incrementing ans by number        # of inversions increase due        # to this swapping        answer += 2 * (right - left) - 1;        left += 1;        right -= 1;     print(answer); # Driver Codeif __name__ == '__main__':         # Input 1    N = 5;    K = 3;    maximum_inversion(N, K);     # Input 2    N = 4;    K = 1;    maximum_inversion(N, K); # This code is contributed by amal kumar choubey

C#

 // C# program for the above approachusing System; class GFG{     // Function which computes the// maximum number of inversionsstatic void maximum_inversion(int n, int k){         // 'answer' will store the required    // number of inversions    int answer = 0;     // We do this because we will    // never require more than    // floor(n/2) operations    k = Math.Min(k, n / 2);     // left pointer in the array    int left = 1;         // right pointer in the array    int right = n;     // Doing k operations    while (k != 0)    {        k--;                 // Incrementing ans by number        // of inversions increase due        // to this swapping        answer += 2 * (right - left) - 1;        left++;        right--;    }    Console.WriteLine(answer);} // Driver Codepublic static void Main(String []s){         // Input 1    int N = 5;    int K = 3;    maximum_inversion(N, K);         // Input 2    N = 4;    K = 1;    maximum_inversion(N, K);}} // This code is contributed by Rohit_ranjan

Javascript


Output:
10
5

Time Complexity: O(K)
Auxillary Space: O(1)

My Personal Notes arrow_drop_up