Problem Brief
Minimize Array Sum Difference
FULLTIMEOA
Given an array 5 4 0 3 3 1, take sum of absolute differences between adjacent pairs i.e |5-4|+|4-0|+|0-3|+|3-3|+|3-1| = 10
The task is to remove as many elements from the array such that the sum remains same.
1Example 1
Input
arr = [5, 4, 0, 3, 3, 1]
Output
[5, 0, 3, 1]
Explanation
Original sum of absolute differences: |5-4|+|4-0|+|0-3|+|3-3|+|3-1| = 10.
After removing elements to minimize the array while keeping the sum the same:
Solution 1: [5, 4, 0, 3, 1] => Sum = 10
Solution 2: [5, 0, 3, 1] => Sum = 10
Solution 2 is the acceptable answer as it has the minimum number of elements.
2Example 2
Input
arr = [6, 4, 4, 3, 3, 2]
Output
[6, 2]
Explanation
:)
IMPORTANT NOTE —
The expected output from the original source is [6, 4, 3, 2].
However, a pro from the server pointed out that [6, 2] would actually make more sense as the correct answer for that example.