DEV Community

Sivakumar Mathiyalagan
Sivakumar Mathiyalagan

Posted on

Interview Question - Technical

Question:

There are N light bulbs, all initially turned OFF (0).

You are given K operations, where each operation is represented as a range [L, R].

For each operation, you must toggle the state of all bulbs in the range:

If a bulb is 0 (OFF), turn it 1 (ON)
If a bulb is 1 (ON), turn it 0 (OFF)

CODE

         let n = 5
         let k = 2
         let operations = [[1,3],[2,4]]

         const arrN = new Array(n).fill(0);
         console.log(arrN);

         for(let i=0;i<k;i++){
            let[start,end] = operations[i]
            console.log(start,end)
            for(let j=start-1;j<end;j++){
                if(arrN[j]===0){
                    arrN[j]=1;
                }
                else{
                    arrN[j] = 0;
                }
            }
         }

         console.log(arrN)
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Top comments (0)