Problem · Array
Range Negation Updates
Learn this problemProblem statement
A data analyst has an integer array data representing values for n days. The analyst performs a sequence of updates. Each update is a pair [l, r] that negates every value from index l through index r, inclusive.
Apply all updates in order and return the final array. Indices in every update are 1-based.
Function
getFinalData(data: int[], updates: int[][]) → int[]Examples
Example 1
data = [1,-4,6,2]updates = [[2,4],[1,2]]return = [-1,-4,-6,-2]After update [2, 4], the array is [1, 4, -6, -2]. Applying [1, 2] then produces [-1, -4, -6, -2].
Example 2
data = [3,-2,5]updates = [[1,1],[1,3]]return = [3,2,-5]The first update negates only the first value. The second update negates all three values, so the first value is negated twice overall while the others are negated once.
Constraints
- Each update contains two valid 1-based indices
landr. - For every update,
l ≤ r, and both endpoints are inclusive.