The logistics framing threw me for a bit longer than it should have.
First, clarify that the problem asks for the minimum number of elements to keep in a subsequence such that the sum of absolute differences between consecutive elements equals that of the original array. Then, observe that the total sum is simply the sum of absolute differences between adjacent elements in the original array, and that any subsequence must include the first and last elements to preserve the total sum. The problem reduces to finding the longest subsequence that preserves the total sum, which is equivalent to removing elements that do not contribute to the sum (i.e., elements that lie between two other elements in a monotonic run). The minimum length is the number of local extrema (including endpoints) in the array.
Pro tip: Think of the array as a sequence of monotonic segments; the sum of absolute differences is preserved if and only if you keep all the turning points (local minima and maxima) and the endpoints. Removing any other element does not change the sum, so the minimal subsequence is exactly the sequence of turning points.
Restate the problem to ensure you understand: you need the smallest subsequence (by number of elements) whose sum of absolute differences between consecutive elements equals that of the original array. Confirm that the subsequence must preserve the order of elements.
Recognize that the total sum of absolute differences is determined by the endpoints and the turning points (local extrema) of the array. Any element that is not a turning point or endpoint can be removed without changing the sum.
Traverse the array and count the number of turning points: start with the first element, then for each subsequent element, if the direction of change (sign of difference) changes from the previous direction, include the previous element as a turning point. Finally, include the last element. The count of these elements is the answer.
Consider arrays of length 1 or 2: for length 1, the sum is 0, so the minimum subsequence length is 1; for length 2, the sum is the absolute difference, so both elements must be kept. Also handle arrays with all equal elements: the sum is 0, so only one element is needed.
The algorithm runs in O(n) time and O(1) extra space. Walk through a few examples to verify, such as [1,3,2,4] (turning points: 1,3,2,4 -> length 4) and [1,2,3,4] (turning points: 1,4 -> length 2).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.