Calculating max distance between array elements in Python


Suppose you are given an array of length n. Choose i numbers in the array such that the absolute difference between the sum of your numbers and the sum of remaining numbers is as large as possible. Your function should return the max possible distance between the two sets of numbers.
For example:

#Given the following:
arr[ ] = [2, 4, 3, 1, 10]
i = 2

Output: 14

Here, we chose #s 1,2 and the sum of the remaining #s (4,3,10) is 17. Therefore, our output is 17 - 3 = 14.


Access restricted

Subscribe to premium account to see the solution.

Get premium now