Maximum inversions in a sequence of 1 to N after performing given operations at most K times | 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 ; Input 1 ; Input 2
function maximum_inversion ( n , k ) { var answer = 0 ; k = Math . min ( k , parseInt ( n / 2 ) ) ; var left = 1 ; var right = n ; while ( k > 0 ) { k -- ; answer += 2 * ( right - left ) - 1 ; left ++ ; right -- ; } document . write ( answer + " " ) ; } var N = 5 ; var K = 3 ; maximum_inversion ( N , K ) ; N = 4 ; K = 1 ; maximum_inversion ( N , K ) ;