#K516. Lighting Optimization Challenge
Lighting Optimization Challenge
Lighting Optimization Challenge
You are given N rooms, each with a switch that can be either ON
or OFF
. The initial state of all switches is represented by a binary string state
of length N (where '1'
indicates the switch is ON
and '0'
indicates it is OFF
). Your task is to determine if it is possible to achieve a configuration where exactly one switch is ON
and all other switches are OFF
, using at most K toggle operations. In a single toggle, you can change the state of one switch.
Note: If no switches are ON
initially, it is impossible to meet the requirement.
Mathematical Formulation:
Let \( c = \) number of switches that are initially ON
. To achieve exactly one ON
switch, you need to toggle \( c - 1 \) of the currently ON
switches to OFF
. The condition for success is:
[ c \ge 1 \quad \text{and} \quad c - 1 \le K ]
inputFormat
The input is given via standard input in the following format:
N state K
Where:
N
is an integer representing the number of rooms (and switches).state
is a binary string of lengthN
representing the initial state of the switches.K
is an integer representing the maximum number of toggle operations allowed.
outputFormat
Output via standard output a single string: YES
if it is possible to achieve exactly one switch ON
(and all others OFF
) within at most K
toggles; otherwise, output NO
.
5
11001
3
YES