Binary String Partitioning
Last Updated :
11 Dec, 2023
Given a Binary String S consisting of N bits, integers K and D. The task is to determine whether the maximum absolute difference among all K disjoint strings in S can be made less than D.
Note: Some of the K strings can be empty, and each character of S can only belong to one of the K strings
Examples:
Input: N = 6, K = 2, D = 5, S = “110001”
Output: YES
Explanation: The strings can be divided into 2 disjoint strings as : “110” and “001” In this case the absolute difference between the count of 0 and the count of one for both disjointed strings will be = |2-1|=1 and |2-1|=1 respectively. The maximum absolute difference among both is 1, Which is less than D=5, Therefore, the output is YES.
Input: N=7, K=5, D=1, S=1010100
Output: NO
Explanation: The S can be divided into 5 strings as follows : (“10”, “10”, “0”, “10”,” “). The absolute difference between the count of ones and zeros is: (0,0,1,0,0) respectively. Max among all absolute differences is 1. Which is the minimum possible and can’t be made less than D=1. Therefore, the output is NO.
Approach: To solve the problem follow the below idea:
The problem is based on Greedy technique. The concept is to divide S into strings into K strings such that the absolute difference should be minimum as possible.
Steps were taken to solve the problem:
- Create a character array to store characters of S.
- Initialize variables ans, min to 0.
- Traverse over character array and follow below-mentioned steps under scope of loop:
- If character is equal to 1, then increment ans else decrement.
- If ans<0, Then made ans as -ans.
- Initialize min as: ans/k.
- If ans%k != 0, Then min++.
- If value of min is less than D then output YES else NO.
Below is the implementation of the above idea:
C++
#include <iostream>
using namespace std;
int main() {
int n = 6;
int k = 2;
int d = 2;
string str = "110011" ;
int ans = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '1' )
ans++;
else
ans--;
}
if (ans < 0)
ans = ans * (-1);
int min = ans / k;
if (ans % k != 0)
min++;
cout << (min < d ? "YES" : "NO" ) << endl;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int n = 6 ;
int k = 2 ;
int d = 2 ;
String str = " 110011 ";
int ans = 0 ;
char [] c = str.toCharArray();
for ( int i = 0 ; i < n; i++) {
if (c[i] == '1' )
ans++;
else
ans--;
}
if (ans < 0 )
ans = ans * (- 1 );
int min = ans / k;
if (ans % k != 0 )
min++;
System.out.println(min < d ? "YES" : "NO");
}
}
|
Python3
n = 6
k = 2
d = 2
str_input = "110011"
ans = 0
for i in range (n):
if str_input[i] = = '1' :
ans + = 1
else :
ans - = 1
ans = abs (ans)
min_operations = ans / / k + ( 1 if ans % k ! = 0 else 0 )
print ( "YES" if min_operations < d else "NO" )
|
C#
using System;
class GFG {
public static void Main( string [] args)
{
int n = 6;
int k = 2;
int d = 2;
string str = "110011" ;
int ans = 0;
char [] c = str.ToCharArray();
for ( int i = 0; i < n; i++) {
if (c[i] == '1' )
ans++;
else
ans--;
}
if (ans < 0)
ans = ans * (-1);
int min = ans / k;
if (ans % k != 0)
min++;
Console.WriteLine(min < d ? "YES" : "NO" );
}
}
|
Javascript
let n = 6;
let k = 2;
let d = 2;
let str = "110011" ;
let ans = 0;
for (let i = 0; i < n; i++) {
if (str[i] === '1' )
ans++;
else
ans--;
}
if (ans < 0)
ans = ans * (-1);
let min = Math.floor(ans / k);
if (ans % k !== 0)
min++;
console.log(min < d ? "YES" : "NO" );
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...