Count minimum right flips to set all values in an array
Last Updated :
01 Nov, 2022
N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, Find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times.
Note: 0 represents the bulb is off and 1 represents the bulb is on.
Examples:
Input: A[] = [0 1 0 1]
Output: 4
Explanation: Press switch 0 : [1 0 1 0]
press switch 1 : [1 1 0 1]
press switch 2 : [1 1 1 0]
press switch 3 : [1 1 1 1]
Input: A[] = [1 0 0 0 0]
Output: 1
Naive Approach:
Keep a count variable and Iterate from 0 to N-1 and check for every i if A[i] is 0 increment count by 1 and switch all values from i to N-1 as 0 to 1 or 1 to 0
Below is the Implementation of above Idea:
C++
#include <bits/stdc++.h>
using namespace std;
int bulbs( int A[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == 0) {
A[i] = 1;
for ( int j = i + 1; j < N; j++) {
if (A[j] == 1)
A[j] = 0;
else
A[j] = 1;
}
count++;
}
}
return count;
}
int main()
{
int states[] = { 0, 1, 0, 1 };
int N = sizeof (states) / sizeof (states[0]);
cout << "The minimum number of switches needed are "
<< bulbs(states, N);
}
|
Java
import java.io.*;
class GFG {
static int bulbs( int [] A, int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
if (A[i] == 0 ) {
A[i] = 1 ;
for ( int j = i + 1 ; j < N; j++) {
if (A[j] == 1 ) {
A[j] = 0 ;
}
else {
A[j] = 1 ;
}
}
count++;
}
}
return count;
}
public static void main(String[] args)
{
int [] states = { 0 , 1 , 0 , 1 };
int N = states.length;
System.out.print(
"The minimum number of switches needed are "
+ bulbs(states, N));
}
}
|
Python3
class GFG :
@staticmethod
def bulbs( A, N) :
count = 0
i = 0
while (i < N) :
if (A[i] = = 0 ) :
A[i] = 1
j = i + 1
while (j < N) :
if (A[j] = = 1 ) :
A[j] = 0
else :
A[j] = 1
j + = 1
count + = 1
i + = 1
return count
@staticmethod
def main( args) :
states = [ 0 , 1 , 0 , 1 ]
N = len (states)
print ( "The minimum number of switches needed are " + str (GFG.bulbs(states, N)), end = "")
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
public class GFG {
static int bulbs( int [] A, int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == 0) {
A[i] = 1;
for ( int j = i + 1; j < N; j++) {
if (A[j] == 1) {
A[j] = 0;
}
else {
A[j] = 1;
}
}
count++;
}
}
return count;
}
static public void Main()
{
int [] states = { 0, 1, 0, 1 };
int N = states.Length;
Console.Write(
"The minimum number of switches needed are "
+ bulbs(states, N));
}
}
|
Javascript
function bulbs(A, N)
{
let count = 0;
for (let i = 0; i < N; i++) {
if (A[i] == 0) {
A[i] = 1;
for (let j = i + 1; j < N; j++) {
if (A[j] == 1)
A[j] = 0;
else
A[j] = 1;
}
count++;
}
}
return count;
}
let states = [ 0, 1, 0, 1 ];
let N = 4;
document.write( "The minimum number of switches needed are "
+ bulbs(states, N));
|
Output
The minimum number of switches needed are 4
Time Complexity: O(N2).
Auxiliary Space: O(1).
Efficient Approach:
Traverse given array from left to right and keep pressing switch for off bulbs. Keep track of the number of switch presses so far. If the number of presses are even, that means the current switch is in its original state else it is in the other state. Depending on what state the bulb is in, we can increment the count of the number of presses.
Below is the Implementation Of above Idea:
C++
#include<bits/stdc++.h>
using namespace std;
int bulbs( int a[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] == 1 && count % 2 == 0)
continue ;
else if (a[i] == 0 && count % 2 != 0)
continue ;
else if (a[i] == 1 && count % 2 != 0)
{
count++;
}
else if (a[i] == 0 && count % 2 == 0)
{
count++;
}
}
return count;
}
int main()
{
int states[] = {0,1,0,1};
int n = sizeof (states)/ sizeof (states[0]);
cout << "The minimum number of switches needed are " << bulbs(states,n);
}
|
Java
import java.util.*;
public class GFG
{
public int bulbs(ArrayList<Integer> a)
{
int count = 0 ;
for ( int i = 0 ; i < a.size(); i++)
{
if (a.get(i) == 1 && count% 2 == 0 )
continue ;
else if (a.get(i) == 0 && count% 2 != 0 )
continue ;
else if (a.get(i) == 1 && count% 2 != 0 )
{
count++;
}
else if (a.get(i) == 0 && count% 2 == 0 )
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
GFG gfg = new GFG();
ArrayList<Integer> states = new ArrayList<Integer>();
states.add( 0 );
states.add( 1 );
states.add( 0 );
states.add( 1 );
System.out.println( "The minimum number of switches" +
" needed are " + gfg.bulbs(states));
}
}
|
Python3
def bulbs(a, n):
count = 0
for i in range (n):
if (a[i] = = 1 and count % 2 = = 0 ):
continue
elif (a[i] = = 0 and count % 2 ! = 0 ):
continue
elif (a[i] = = 1 and count % 2 ! = 0 ):
count + = 1
elif (a[i] = = 0 and count % 2 = = 0 ):
count + = 1
return count
states = [ 0 , 1 , 0 , 1 ]
n = len (states)
print ( "The minimum number of switches needed are" , bulbs(states, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public virtual int bulbs(List< int > a)
{
int count = 0;
for ( int i = 0; i < a.Count; i++)
{
if (a[i] == 1 && count % 2 == 0)
{
continue ;
}
else if (a[i] == 0 && count % 2 != 0)
{
continue ;
}
else if (a[i] == 1 && count % 2 != 0)
{
count++;
}
else if (a[i] == 0 && count % 2 == 0)
{
count++;
}
}
return count;
}
public static void Main( string [] args)
{
GFG gfg = new GFG();
List< int > states = new List< int >();
states.Add(0);
states.Add(1);
states.Add(0);
states.Add(1);
Console.WriteLine( "The minimum number of switches" +
" needed are " + gfg.bulbs(states));
}
}
|
Javascript
<script>
function bulbs(a, n)
{
let count = 0;
for (let i = 0; i < n; i++)
{
if (a[i] == 1 && count % 2 == 0)
continue ;
else if (a[i] == 0 && count % 2 != 0)
continue ;
else if (a[i] == 1 && count % 2 != 0)
{
count++;
}
else if (a[i] == 0 && count % 2 == 0)
{
count++;
}
}
return count;
}
let states = [0,1,0,1];
let n = states.length;
document.write( "The minimum number of switches needed are " + bulbs(states,n));
</script>
|
Output
The minimum number of switches needed are 4
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...