Maximum absolute difference

Question

Suppose you're given an array of integers of length N. Write a function using Python to select j #s that will maximize the absolute difference between the j numbers chosen and those remaining in the array.

Examples:


Input: arr[ ] = [2, 6, 2, 1, 10]
j = 2
#Output: Here 1,2 are selected, with the difference between the sum of j (3) 
#and the remaining #s (6+10+2 = 5) being 15

Input: arr[ ] = [1, 4, 3, 2, 4]
j = 4
#Output: Here we would select 4, 4, 3, and 2 for a sum of 13, whereas the sum of the remaining # is 1.
#Therefore, the difference between j and our remaining # is 12

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now