Turn Off Lights
Practice
4.4 (8 votes)
String algorithms
Algorithms
String searching
Problem
74% Success 1518 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

There are \(n\) bulbs arranged in a row. The state of the bulbs is represented by a binary string bulbs of length \(n\), the \(0\) at \(i^{th}\) position in the string represents the bulb is OFF and \(1\) represents the bulb is ON, where \(0 \leq i < n\). You have to switch OFF all the bulbs by performing following operation atmost \(k\) times. The operation is defined as:-

  • Choose any index \(i\) in the string bulb and turn OFF all the lights having indexes \(i\) to \(\min(n - 1, i + l - 1)\) (inclusive), where \(l\) is a pre defined number, greater than zero and fixed for all the operations.

The task is to find the smallest value of \(l\) greater than zero, such that you can turn OFF all the bulbs in atmost \(k\) operations.

Input format

  • The first line of input contains two space separated integers, \(n\) and \(k\), representing the size of string bulb and maximum number of operations you can perform.
  • The second line of input contains a binary string, bulbs.

Output format

The output contains a single integer, the minimum possible value of \(l\) greater than zero such that you are able to turn OFF all the bulbs in atmost \(k\) operations.

Constraints

\(1 \leq n \leq 10^6 \\ 1 \leq k \leq n\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
9 votes
Tags:
String SearchingGreedy AlgorithmsAlgorithmsStringString Algorithms
Points:30
3 votes
Tags:
AlgorithmsString SearchingString AlgorithmsC++
Points:30
9 votes
Tags:
String AlgorithmsAlgorithmsC++String Searching