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 = 3Output:10Explanation:

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 10Input:N = 4, K = 1Output:5Explanation:

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.

- On, swapping
- Print the value of answer

Below is the implementation of the above approach:

## C++

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

*filter_none*

## Java

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

*filter_none*

## Python3

`# 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*

*filter_none*

## C#

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

*filter_none*

**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.

## Recommended Posts:

- Find the final sequence of the array after performing given operations
- Maximum Possible Product in Array after performing given Operations
- Maximum count of equal numbers in an array after performing given operations
- Maximum sum of all elements of array after performing given operations
- Maximum number of unique values in the array after performing given operations
- Maximum score possible after performing given operations on an Array
- Maximum possible Array sum after performing given operations
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Maximize length of subarray of equal elements by performing at most K increment operations
- Minimum element left from the array after performing given operations
- Find the modified array after performing k operations of given type
- Number of elements from the array which are reachable after performing given operations on D
- Check if the last element of array is even or odd after performing a operation p times
- Check if original Array is retained after performing XOR with M exactly K times
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Number of array elements derivable from D after performing certain operations
- Maximum value obtained by performing given operations in an Array
- Maximum possible array sum after performing the given operation
- Find original sequence from Array containing the sequence merged many times in order
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]

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.