Given a binary array arr[], where 0 represents a bulb that is off and 1 represents a bulb that is on, find the minimum number of switches required to turn on all the bulbs. Pressing the switch of the ith bulb toggles the state of that bulb and every bulb to its right.
Return the minimum number of switches required to make all bulbs on.
Examples:
Input: arr[] = [0, 0, 0, 0]
Output: 1
Explanation: Initially: [0, 0, 0, 0]
Press the switch of the first bulb. All bulbs are toggled: [0, 0, 0, 0] -> [1, 1, 1, 1].
All bulbs are now on, so the answer is 1.Input: arr[] = [1, 0, 0, 1]
Output: 2
Explanation: Initially: [1, 0, 0, 1]
Press the switch of the second bulb: [1, 0, 0, 1] -> [1, 1, 1, 0]
Press the switch of the fourth bulb: [1, 1, 1, 0] -> [1, 1, 1, 1]
All bulbs are now on, so the answer is 2.
Table of Content
[Naive Approach] Simulate Every Switch Press - O(n^2) Time and O(1) Space
The idea is to process the bulbs from left to right. Whenever an off bulb is encountered, press its switch to turn it on. Since pressing a switch toggles the current bulb and every bulb to its right, explicitly simulate this operation by flipping the state of each affected bulb.
Working of Approach:
- Traverse the array from left to right.
- If the current bulb is already on, move to the next bulb.
- Otherwise, increment the switch press count.
- Toggle the state of every bulb from the current index to the end of the array.
- Continue the process until all bulbs have been processed.
- Return the total number of switch presses.
#include <iostream>
#include <vector>
using namespace std;
int countFlips(vector<int>& arr) {
int cnt = 0;
int n = arr.size();
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
cnt++;
// Toggle all bulbs to the right
for (int j = i; j < n; j++)
arr[j] ^= 1;
}
}
return cnt;
}
int main() {
vector<int> arr = {0, 0, 0, 0};
cout << countFlips(arr);
return 0;
}
class GFG {
static int countFlips(int[] arr) {
int cnt = 0;
int n = arr.length;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
cnt++;
// Toggle all bulbs to the right
for (int j = i; j < n; j++)
arr[j] ^= 1;
}
}
return cnt;
}
public static void main(String[] args) {
int[] arr = {0, 0, 0, 0};
System.out.println(countFlips(arr));
}
}
def countFlips(arr):
cnt = 0
n = len(arr)
for i in range(n):
if arr[i] == 0:
cnt += 1
# Toggle all bulbs to the right
for j in range(i, n):
arr[j] ^= 1
return cnt
if __name__ == "__main__":
arr = [0, 0, 0, 0]
print(countFlips(arr))
using System;
class GFG {
static int countFlips(int[] arr) {
int cnt = 0;
int n = arr.Length;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
cnt++;
// Toggle all bulbs to the right
for (int j = i; j < n; j++)
arr[j] ^= 1;
}
}
return cnt;
}
static void Main() {
int[] arr = {0, 0, 0, 0};
Console.WriteLine(countFlips(arr));
}
}
function countFlips(arr) {
let cnt = 0;
let n = arr.length;
for (let i = 0; i < n; i++) {
if (arr[i] === 0) {
cnt++;
// Toggle all bulbs to the right
for (let j = i; j < n; j++)
arr[j] ^= 1;
}
}
return cnt;
}
// Driver Code
let arr = [0, 0, 0, 0];
console.log(countFlips(arr));
Output
1
[Expected Approach] Keep Track of Flip Parity - O(n) Time and O(1) Space
The idea is to avoid toggling the bulbs explicitly. Instead, keep track of whether the bulbs have been flipped an even or odd number of times while traversing the array. Using this information, determine the current state of each bulb. If a bulb is off, press its switch, increment the answer, and update the flip state.
Working of Approach:
- Initialize a variable to count the number of switch presses and another variable to track the parity (even or odd) of flips performed so far.
- Traverse the array from left to right.
- Determine the current state of the bulb based on the flip parity.
- If the current bulb is off, increment the switch press count and toggle the flip parity.
- Continue this process until all bulbs have been processed.
- Return the total number of switch presses.
#include <iostream>
#include <vector>
using namespace std;
int countFlips(vector<int>& arr) {
int cnt = 0;
bool flipped = false;
for (int bulb : arr) {
int curr = flipped ? 1 - bulb : bulb;
if (curr == 0) {
cnt++;
flipped = !flipped;
}
}
return cnt;
}
int main() {
vector<int> arr = {0, 0, 0, 0};
cout << countFlips(arr);
return 0;
}
class GFG {
static int countFlips(int[] arr) {
int cnt = 0;
boolean flipped = false;
for (int bulb : arr) {
int curr = flipped ? 1 - bulb : bulb;
if (curr == 0) {
cnt++;
flipped = !flipped;
}
}
return cnt;
}
public static void main(String[] args) {
int[] arr = {0, 0, 0, 0};
System.out.println(countFlips(arr));
}
}
def countFlips(arr):
cnt = 0
flipped = False
for bulb in arr:
curr = 1 - bulb if flipped else bulb
if curr == 0:
cnt += 1
flipped = not flipped
return cnt
if __name__ == "__main__":
arr = [0, 0, 0, 0]
print(countFlips(arr))
using System;
class GFG {
static int countFlips(int[] arr) {
int cnt = 0;
bool flipped = false;
foreach (int bulb in arr) {
int curr = flipped ? 1 - bulb : bulb;
if (curr == 0) {
cnt++;
flipped = !flipped;
}
}
return cnt;
}
static void Main() {
int[] arr = {0, 0, 0, 0};
Console.WriteLine(countFlips(arr));
}
}
function countFlips(arr) {
let cnt = 0;
let flipped = false;
for (let bulb of arr) {
let curr = flipped ? 1 - bulb : bulb;
if (curr === 0) {
cnt++;
flipped = !flipped;
}
}
return cnt;
}
// Driver Code
let arr = [0, 0, 0, 0];
console.log(countFlips(arr));
Output
1