#K47227. Maximum Binary Number After Bit Flips
Maximum Binary Number After Bit Flips
Maximum Binary Number After Bit Flips
Given a binary string s
and an integer k
, transform s
into the maximum possible binary number by flipping at most k
'0's to '1's. The process is performed from left to right: whenever a '0' is encountered and you still have remaining flips, change it to '1'.
In mathematical terms, if s = s_1 s_2 ... s_n
and k
is the maximum number of changes, then the result r
is obtained by:
\( r_i = \begin{cases} 1, & \text{if } s_i = 0 \text{ and the number of flips used so far is less than } k \\ s_i, & \text{otherwise} \end{cases} \)
Output the resulting binary string after performing the operations.
inputFormat
The input consists of two lines:
- The first line contains the binary string
s
(a sequence of characters '0' and '1'). - The second line contains an integer
k
indicating the maximum number of bit flips allowed.
outputFormat
Output a single line containing the maximum binary string obtained after flipping at most k
zeros to ones.
010101
2
111101