Maximum inversions in a sequence of 1 to N after performing given operations at most K times | Java program for the above approach ; Function which computes the maximum number of inversions ; ' answer ' will store the required number of inversions ; We do this because we will never require more than floor ( n / 2 ) operations ; left pointer in the array ; right pointer in the array ; Doing k operations ; Incrementing ans by number of inversions increase due to this swapping ; Driver Code ; Input 1 ; Input 2
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 ) ; } }