Get the Fewest Moves (~Operations~)~
Learn this problemProblem statement
You are given an integer list named data. The objective is to adjust all elements in this list so that each becomes zero. You can carry out the following move any number of times:
--> Pick a prefix portion of the list and either increase or decrease every element in that prefix by 1.
Your task is to figure out the fewest moves required to turn all the elements into zero.
Output - Print a single number — the minimum number of steps needed.
A prefix segment refers to a continuous collection of elements starting from the first item in the sequence. For instance, [1], [1, 2], [1, 2, 3], and so on are valid prefixes of the sequence [1, 2, 3, 4, 5].
It is assured that turning all values in the list to zero is always achievable.
Update 06-26-2026: This problem has been confirmed as a duplicate of Minimum Operations to Make the Integer Zero. I recommend practicing that version because it is more complete. I merged this page's last seen dates into that problem, so I will not keep tracking this duplicate separately going forward. 🐬
Function
getMinimumOperations(d: int[]) → intExamples
Example 1
d = [3, 2, 1]return = 3Example 2
d = [3, 2, 0, 0, -1]return = 5Constraints
1 ≤ n ≤ 10^5-10^9 ≤ d[i] ≤ 10^9
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026