Open In App

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

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 
 

 

  1. Print the value of answer

Below is the implementation of the above approach:
 




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function which computes the
// maximum number of inversions
int 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 code
int 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 program for the above approach
import java.util.*;
 
class GFG{
     
// Function which computes the
// maximum number of inversions
static 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 Code
public 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 program for the above approach
 
# Function which computes the
# maximum number of inversions
def 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 Code
if __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# program for the above approach
using System;
 
class GFG{
     
// Function which computes the
// maximum number of inversions
static 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 Code
public 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




<script>
// javascript program for the above approach
 
    // Function which computes the
    // maximum number of inversions
    function maximum_inversion(n, k) {
 
        // 'answer' will store the required
        // number of inversions
        var answer = 0;
 
        // We do this because we will
        // never require more than
        // floor(n/2) operations
        k = Math.min(k, parseInt(n / 2));
 
        // left pointer in the array
        var left = 1;
 
        // right pointer in the array
        var 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--;
        }
        document.write(answer + "<br/>");
    }
 
    // Driver Code
 
        // Input 1
        var N = 5;
        var K = 3;
        maximum_inversion(N, K);
 
        // Input 2
        N = 4;
        K = 1;
        maximum_inversion(N, K);
 
// This code is contributed by aashish1995
</script>

Output: 
10
5

 

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


Article Tags :