Minimize Max Frequency Difference of 0 & 1 by Dividing Binary String into K Disjoint Subsequence


In this problem, we will divide the given binary string into the K subsequences such that we can minimize the maximum absolute difference of count of ‘1’ and ‘0’ in the given string.

The logic to solve the problem is to create a maximum pair of 0 and 1 in subsequences. So, we can get the minimum difference between each subsequence.

Problem statement − We have given a bin_str binary string of length N. We have also given the positive integer K. We need to divide the given string into the K disjoint subsequences to minimize the maximum absolute difference of count of ‘0’ and ‘1’ in each subsequence. It is also allowed us to take the empty subsequences. In the output, print the minimum difference.

Note − We can create the disjoint subsequence using the non−adjacent characters of the given string.

Sample examples

Input

bin_str = "110011", K = 3

Output

1

Explanation − We can divide the string into the ‘10’, ‘11’, and ‘01’ subsequences, as we need to take disjoint subsequences.

In each subsequence, a difference of a number of ‘1’ and ‘0’ is {1, 2, 1}. So, the maximum difference is 2 – 1 is equal to 1.

Input

K = 2;   string bin_str = "111011";

Output

2

Explanation − The 2 disjoint subsequences can be [‘111’, ‘101’]. So, a difference of ‘1’ and ‘0’ in each subsequence is {3, 1}. The maximum absolute difference is 2.

Approach 1

We can get the minimum difference among all subsequences if we can divide the total difference between all subsequences equally. So, we need to find the difference between a number of ‘0’ and ‘1’ in the given string. After that, we can divide the difference with the given K and take its ceil value for the answer.

Algorithm

Step 1 − Initialize the ‘cnt0’ and ‘cnt1’ to store the ‘0’ and ‘1’ count in the given string.

Step 2 − Traverse the given binary string. If the current character is ‘0’, increment the ‘cnt0’ by 1. Otherwise, increment the ‘cnt1’ by 1.

Step 3 − Take the absolute difference between the ‘cnt1’ and ‘cnt0’.

Step 4 − Return the ceil value after dividing the difference by K.

Example

#include <bits/stdc++.h>
using namespace std;

int getMinDiff(string bin_str, int str_len, int K) {
    int cnt0 = 0, cnt1 = 0;
    // Count 0 and 1 in the binary string
    for (int p = 0; p < str_len; ++p) {
        if (bin_str[p] == '0')
            cnt0++;
        else
            cnt1++;
    }
    // Get the absolute difference between 0 and 1 count
    int difference = abs(cnt1 - cnt0);
    // Get ceil of difference and return it
    return (difference + K - 1) / K;
}
int main() {
    int K = 3;
    string bin_str = "110011";
    int str_len = bin_str.length();
    cout << "The absolute minimum difference after dividing the binary string into K substrings is " << getMinDiff(bin_str, str_len, K) << endl;
    return 0;
}

Output

The absolute minimum difference after dividing the binary string into K substrings is 1

Time complexity − O(N) for the count number of ‘1’ and ‘0’ in the binary string.

Space complexity − O(1)

We learned to divide the given string into the K disjoint subsequences to minimize the maximum absolute difference of ‘1’ and ‘0’ among given subsequences. However, programmers may use the dynamic programming approach to solve the problem.

Updated on: 17-Jul-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements