Problem 2.4: calculate number of inversions