Generation

generate functionTue, 13 Dec 2022

[2, 1, 10] --> 9 In descending order: [10, 2, 1] Sum: (10 - 2) + (2 - 1) = 8 + 1 = 9 If the array is empty or the array has only one element the result should be 0 (Nothing in Haskell, None in Rust).

def sum_of_differences(arr): if len(arr) <= 1: return 0 arr.sort(reverse=True) return sum(arr[i] - arr[i + 1] for i in range(len(arr) - 1))

Questions about programming?Chat with your personal AI assistant