Minimize max frequency difference of 0 & 1 by dividing Binary String into K disjoint Subsequence
Given a binary string S of size N and an integer K, the task is to divide S into K disjoint subsequences(empty subsequences are also allowed) such that the maximum absolute difference between the number of 0s and 1s among these subsequences is minimized.
Example:
Input: N = 7, K = 5, S = “1010100”
Output: 1
Explanation: Suppose the partition is {“10”, “10”, “0”, “10”, ” “}.
The respective absolute difference between
number of ones and zeroes are {0, 0, 1, 0, 0}.
The maximum of which is 1.
Input: N = 6, K = 2, S = “101111”
Output: 2
Approach: The problem can be solved using the below mathematical idea:
Find the absolute difference between number of zeroes and ones present in the binary string, divide it in K parts by taking value of ceil(difference/K)
Follow the steps to implement this idea:
- Initialize variables ones and zeros both to 0.
- Iterate over the string and increase the count of ones and zeros according to the characters of the string.
- Find the absolute difference between ones and zeros and store it in variable diff.
- Return ceil(diff/K).
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minAbsDiff(string S, int N, int K)
{
int zeros = 0, ones = 0;
for ( int i = 0; i < N; ++i) {
if (S[i] == '0' )
++zeros;
else
++ones;
}
int diff = abs (ones - zeros);
int ans = (diff + K-1) / K;
return ans;
}
int main()
{
int N = 7;
int K = 5;
string S = "1010100" ;
int ans = minAbsDiff(S, N, K);
cout << ans << endl;
}
|
Java
class GFG {
public static int minAbsDiff(String S,
int N, int K)
{
int zeros = 0 , ones = 0 ;
for ( int i = 0 ; i < N; ++i) {
if (S.charAt(i) == '0' )
++zeros;
else
++ones;
}
double diff = Math.abs(ones - zeros);
int ans = ( int )Math.ceil(diff / K);
return ans;
}
public static void main(String[] args)
{
int N = 7 ;
int K = 5 ;
String S = "1010100" ;
int ans = minAbsDiff(S, N, K);
System.out.println(ans);
}
}
|
Python3
def minAbsDiff(S, N, K) :
zeros = 0 ; ones = 0 ;
for i in range (N) :
if (S[i] = = '0' ) :
zeros + = 1 ;
else :
ones + = 1 ;
diff = abs (ones - zeros);
ans = (diff + K - 1 ) / / K;
return ans;
if __name__ = = "__main__" :
N = 7 ;
K = 5 ;
S = "1010100" ;
ans = minAbsDiff(S, N, K);
print (ans);
|
C#
using System;
public class GFG {
public static int minAbsDiff(String S, int N, int K)
{
int zeros = 0, ones = 0;
for ( int i = 0; i < N; ++i) {
if (S[i] == '0' )
++zeros;
else
++ones;
}
double diff = Math.Abs(ones - zeros);
int ans = ( int )Math.Ceiling(diff / K);
return ans;
}
static public void Main()
{
int N = 7;
int K = 5;
String S = "1010100" ;
int ans = minAbsDiff(S, N, K);
Console.WriteLine(ans);
}
}
|
Javascript
function minAbsDiff(S, N, K) {
let zeros = 0,
ones = 0;
for (let i = 0; i < N; ++i) {
if (S[i] == "0" ) ++zeros;
else ++ones;
}
let diff = Math.abs(ones - zeros);
let ans = (diff + K - 1) / K;
return ans;
}
let N = 7;
let K = 5;
let S = "1010100" ;
let ans = minAbsDiff(S, N, K);
console.log(ans);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
02 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...