Solutions for Peter and the job

    • @DarrenNop
    • @william, submitted in Python 3, achived 100% score
    def sort_pair(arr0, arr1):
        if len(arr0) > len(arr1):
            return arr1, arr0
        else:
            return arr0, arr1
    
    def merge(arr0, arr1):
        inversions = 0
        result = []
        while len(arr0) > 0 and len(arr1) > 0:
            if arr0[0] <= arr1[0]:
                result.append(arr0.pop(0))
            else:
                inversions += len(arr0)
                result.append(arr1.pop(0))
    
        if len(arr0) == 0:
            result += arr1
        elif len(arr1) == 0:
            result += arr0
    
        return result, inversions
    
    def sort(arr):
        length = len(arr)
        mid = length//2
        if length >= 2:
            sorted_0, counts_0 = sort(arr[:mid])
            sorted_1, counts_1 = sort(arr[mid:])
            result, counts = merge(sorted_0, sorted_1)
            return result, counts + counts_0 + counts_1
        else:
            return arr, 0
    
    def solve(a):
        final_array, inversions = sort(a)
        return inversions