L17 - Longest Sequence of 1 After Flipping A Bit
L17 - Longest Sequence of 1 After Flipping A Bit
❑ The aim is to identify the configuration that yields the maximum length of
consecutive ones within the allowed number of flips.
❑ It's important to note that the number of allowed flips can greatly impact the
result. A higher number of flips generally allows for a longer sequence of ones,
while a limited number of flips may result in a shorter sequence.
Example: 11011101111
Step 1:
11011101111
11111101111
Flipping the First 0 results in 11111101111
length of the sequence of consecutive 1s=6
Step2:
11011101111
11011111111
1110001111
1111101111
1110001111
1110111111
How would you modify the solution if you were allowed to flip
any number of 0s?
If we can flip any number of 0s, we can simply count the number of
0s encountered and keep track of the maximum length of the
sequence of 1s seen so far. Whenever we encounter a 0, we update
the maximum length if necessary and reset the count of 0s.
Interview questions
What is the space complexity of the given algorithm to find the
longest sequence of 1s after flip?