0 out of 464 challenges solved
An inversion in an array is a pair of elements `(arr[i], arr[j])` such that `i < j` and `arr[i] > arr[j]`. Write a Python function `count_inversions` that takes a list of integers as input and returns the number of inversions in the array. #### Example Usage ```python [main.nopy] print(count_inversions([1, 20, 6, 4, 5])) # Output: 5 print(count_inversions([1, 2, 1])) # Output: 1 print(count_inversions([1, 2, 5, 6, 1])) # Output: 3 ``` #### Constraints - The input list will contain integers. - The list can be empty, in which case the function should return `0`.
def count_inversions(arr): """ Count the number of inversions in the array. Args: arr (list): A list of integers. Returns: int: The number of inversions in the array. """ # Initialize the inversion count inversion_count = 0 # Implement the logic to count inversions # Placeholder for the solution return inversion_count