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 )

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

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: 

10
5

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.