Count inversions in a sequence generated by appending given array K times | Function to count the number of inversions in K copies of given array ; Stores count of inversions in the given array ; Stores the count of pairs of distinct array elements ; Traverse the array ; Generate each pair ; Check for each pair , if the condition is satisfied or not ; If pairs consist of distinct elements ; Count inversiosn in the sequence ; Prthe answer ; Given array ; Given K ; Size of the array
def totalInversions ( arr , K , N ) : NEW_LINE INDENT inv = 0 NEW_LINE X = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] and i < j ) : NEW_LINE INDENT inv += 1 NEW_LINE DEDENT if ( arr [ i ] > arr [ j ] ) : NEW_LINE INDENT X += 1 NEW_LINE DEDENT DEDENT DEDENT totalInv = X * K * ( K - 1 ) // 2 + inv * K NEW_LINE print ( totalInv ) NEW_LINE DEDENT arr = [ 2 , 1 , 3 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE totalInversions ( arr , K , N ) NEW_LINE