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 Description
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༉˚
Example 1:
Input: n = 7, operations = [[1, 4], [2, 6], [1, 6]]
Output: 9
Explanation:3 operations are performed on a row of switches that are all off initially. In the figure below, '-' represents a switch that is off, and an 'X' represents one that is on.After all operations, there are switches in the on position at indices 2, 3, and 4. The sum of the indices is 2 + 3 + 4 = 9.
(˶˃ ᵕ ˂˶) .ᐟ.ᐟ
input:
output: