Problem · Array
Sum of On Switch Indices
Learn this problemProblem statement
Switches are numbered with positive consecutive indices and initially are off. Each operation [left, right] toggles every switch in that inclusive interval.
After every operation has been applied, return the sum of the indices of all switches that are on.
Function
sumOnSwitchIndices(operations: int[][]) → longExamples
Example 1
operations = [[1,4],[2,6],[1,6]]return = 9Only switches 2, 3, and 4 finish on, so the sum is 9.
Constraints
1 <= operations.length <= 2000001 <= left <= right <= 10^9- The answer fits a signed
long.