Problem · Array

Get Final Data

Learn this problem
MediumIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

Given an integer array data and a list of updates, apply every update in order.

Each update is a 1-based inclusive range [l, r]. Applying it negates every value from position l through position r.

Return the final array after all updates.

Function

getFinalData(data: int[], updates: int[][]) → int[]

Examples

Example 1

data = [1, -4, -5, 2]updates = [[2, 4], [1, 2]]return = [-1, -4, 5, -2]

After [2, 4], the data is [1, 4, 5, -2]. After [1, 2], it becomes [-1, -4, 5, -2].

Constraints

  • 1 ≤ data.length ≤ 10^5
  • 1 ≤ updates.length ≤ 10^5
  • -10^9 ≤ data[i] ≤ 10^9
  • Every update satisfies 1 ≤ l ≤ r ≤ data.length.

More IBM problems

drafts saved locally
public int[] getFinalData(int[] data, int[][] updates) {
    // Write your code here
}
data[1, -4, -5, 2]
updates[[2, 4], [1, 2]]
expected[-1, -4, 5, -2]
checking account