Problem · Array

Sum of On Switch Indices

Learn this problem
MediumAtlassian logoAtlassianFULLTIMEOA

Problem 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[][]) → long

Examples

Example 1

operations = [[1,4],[2,6],[1,6]]return = 9

Only switches 2, 3, and 4 finish on, so the sum is 9.

Constraints

  • 1 <= operations.length <= 200000
  • 1 <= left <= right <= 10^9
  • The answer fits a signed long.

More Atlassian problems

drafts saved locally
public long sumOnSwitchIndices(int[][] operations) {
  // Write your code here.
}
operations[[1,4],[2,6],[1,6]]
expected9
checking account