Kotlinlearncs.online LogoJava
Return to List

Solve: Array Count Inversions

Created By: Pranav Narayanan
/ Version: 2025.3.0

Declare and implement a function called countArrayInversions that receives an array of Int values and returns a count of the number of inversions in the array. An inversion is a pair of indices i and j where i < j but A[i] > A[j], for the array A.

Note: this can be completed with a nested for loop, but a faster algorithm can be achieved using recursion and a modified merge sort. If you choose to do this, note that you cannot modify the input array, so you may need to copy it. You may also want to write a private helper function, like so:

Related Lessons

Stuck? You may find these lessons helpful:

Report a Problem

Attribution must link to this page: https://www.learncs.online/solve/kotlin/array-count-inversions/[email protected]