Calculate the Sum
Learn this problemProblem statement
There is a row of two-position switches aligned in a row and numbered consecutively starting from 1. Each of the switches is initially in its "Off" position. Over some number of operations, a left and right index will be provided. When a current is applied to two switches, a NOT operation is applied to each switch in the inclusive interval between those switches. That is, if a switch is off, it is turned on, and vice versa. Given a series of operations, determine their final state. Calculate the sum of all indices where a switch is on.
Function
calculateTheSum(n: int, operations: int[][]) → int
Complete the function calculateTheSum in the editor.
calculateTheSum has the following parameter:
int n: the number of operationsint[][] operations: a 2D array of integers where each element is an array of two integers representing the left and right indices for the operation
Returns
int: the sum of all indices where a switch is on
🌷ଓ Credit to Charlotte༉˚
Examples
Example 1
n = 7operations = [[1, 4], [2, 6], [1, 6]]return = 9
Constraints
(˶˃ ᵕ ˂˶) .ᐟ.ᐟ