-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bulbs.java
30 lines (28 loc) · 926 Bytes
/
Bulbs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Problme definition
// Given N Bulbs ,Either ON or OFF.
// Turning on ith bulb causes all remaining bulbs on the right to flip.
// Find Minimum Number of switches to turn all the bilbs on.
import java.util.Scanner;
public class Bulbs {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
int number_of_Bulbs = Input.nextInt();
int[] states_of_Bulbs = new int[number_of_Bulbs];
for(int i=0;i<number_of_Bulbs;i++)
{
states_of_Bulbs[i] = Input.nextInt();
}
int cost=0;
for(int i=0;i<number_of_Bulbs;i++)
{
if ((cost&1)!=0) {
states_of_Bulbs[i]=1-states_of_Bulbs[i];
}
if (states_of_Bulbs[i]==0) {
cost++;
}
}
System.out.println("Total cost of turning on all bulbes is: " + cost);
Input.close();
}
}