Merge step counts cross-inversions while merging sorted halves. When , all remaining elements form inversions with . Add L.length - i to count.
while i < L.length and j < R.length:
if L[i] <= R[j]:
arr[k] = L[i]; i++
else:
arr[k] = R[j]
inversions += L.length - i
j++
k++
Copy remaining elements after the loop.