Maximize sum of assigned weights by flipping at most K bits in given Binary String

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.

Examples:

Input: str = “10100”, K = 2
Output: 7
Explanation:
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
Output: 6
Explanation:
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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find maximum sum of
// weights of binary string after
// at most K flips
int findMax(string s, int n, int k)
{
    int ans = 0;
  
    // Stores lengths of substrings
    // of the form 1..00..1s
    int l = 0;
  
    // Stores the index of last 1
    // encountered in the string
    int ind = -1;
  
    // Stores the index of first 1
    // encountered
    int indf = -1;
  
    // Stores lengths of all substrings
    // having of 0s enclosed by 1s
    // at both ends
    multiset<int> ls;
  
    // Traverse the string
    for (int i = 0; i < n; i++) {
        // If character is 0
        if (s[i] == '0')
            l++;
  
        // If character is 1
        // First Priority
        else if (s[i] == '1'
                 && l > 0 && ans != 0) {
            ls.insert(l);
            l = 0;
        }
  
        // Second Priority
        if (s[i] == '1') {
            ind = i;
            l = 0;
            if (indf == -1)
                indf = i;
  
            // Add according to the
            // first priority
            if (i > 0 && s[i - 1] == '1')
                ans += 2;
            else
                ans += 1;
        }
    }
  
    // Stores length of the shortest
    // substring of 0s
    int curr;
  
    // Convert shortest substrings
    // of 0s to 1s
    while (k > 0 && !ls.empty()) {
        curr = *ls.begin();
  
        // Add according to the
        // first priority
        if (k >= curr) {
            ans += (2 * curr + 1);
            k -= curr;
        }
  
        // Add according to the
        // third priority
        else {
            ans += (2 * k);
            k = 0;
        }
        ls.erase(ls.begin());
    }
  
    // If more 0s can be made into 1,
    // then check for 0s at ends
    if (k > 0) {
        // Update the ans
        ans += (2 * min(k,
                        n - (ind + 1))
                - 1);
        k -= min(k, n - (ind + 1));
  
        if (ind > -1)
            ans++;
    }
  
    // If K is non-zero, then flip 0s
    // at the beginning
    if (k > 0) {
        ans += (min(indf, k) * 2 - 1);
  
        if (indf > -1)
            ans++;
    }
  
    // Return the final weights
    return ans;
}
  
// Driver Code
int main()
{
    // Given string str
    string str = "1110000101";
  
    int N = str.length();
  
    // Given K flips
    int K = 3;
  
    // Function Call
    cout << findMax(str, N, K);
  
    return 0;
}

chevron_right


Output:

14

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.