Given a binary string str of length N and an integer K, the task is to find the maximum possible sum of assigned weights that can be obtained by flipping at most K bits in the given binary string. The weight assigned to the characters of this string are as follows:
- If a character is ‘0’, then the weight is 0.
- If a character is ‘1’ and the character preceding it is also ‘1’, then the weight is 2.
- If a character is ‘1’ and there is no character before it or the character preceding it is ‘0’, then the weight is 1.
Input: str = “10100”, K = 2
1st flip: Flip the character at index 1, the string becomes “11100”.
2nd flip: Flip the character at index 3, the string becomes “11110”.
The weight of the resulting string is 1 + 2 + 2 + 2 + 0 = 7, which is maximum.
Input: str = “100101”, K = 1
1st flip: Flip the character at index 5, the string becomes “100111”.
The weight of the resulting string is 1 + 0 + 0 + 1 + 2 + 2 = 6, which is maximum.
Approach: The weight of character ‘1’ appearing after a ‘1’ is greatest among all characters, so to maximize the sum, try to create as many such 1s possible. The segments of 0s to be flipped to 1 can be prioritized as follows:
- First priority: Flip all 0s enclosed between two 1s, this would increase the weight of the segment of form 10…01 by (2*(number of 0s enclosed) + 1). If x is greater than or equal to the number of 0s enclosed otherwise by 2*(number of 0s enclosed).
- Second priority” Flip 0s at the beginning of the string preceding the first occurrence of 1s in the string, this would increase the weight of segment of form 0…01 by 2*(number of flipped 0s).
- Third priority: Flip 0s at the end of the string succeeding the last occurrence of 1 in the string, this would increase the weight of the segment of form 10…0 by 2*(number of flipped 0s).
Flip the character of the given string as per the above priority to maximize the weight and then find the weight of the resulting string after at most K flips.
Below is the implementation of this approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize the number by flipping at most K bits
- Find optimal weights which can be used to weigh all the weights in the range [1, X]
- Check if all bits can be made same by flipping two consecutive bits
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Count of all sub-strings with sum of weights at most K
- Maximum score assigned to a subsequence of numerically consecutive and distinct array elements
- Numbers formed by flipping common set bits in two given integers
- Maximize sum of an Array by flipping sign of all elements of a single subarray
- Number formed by flipping all bits to the left of rightmost set bit
- Maximize the decimal equivalent by flipping only a contiguous set of 0s
- Maximize count of rows consisting of equal elements by flipping columns of a Matrix
- Minimum cost of flipping characters required to convert Binary String to 0s only
- Balance pans using given weights that are powers of a number
- Binary Matrix after flipping submatrices in given range for Q queries
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Dial's Algorithm (Optimized Dijkstra for small range weights)
- Maximum score after flipping a Binary Matrix atmost K times
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Minimize cost to convert a given matrix to another by flipping columns and reordering rows
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.