Maximise length of smallest group by dividing Binary String into K groups of same bit
Last Updated :
21 Sep, 2023
Given a binary string S of length N. Distribute 0s and 1s of the string into K groups such that:
- Each group consists atleast a character.
- No group contains both 1 and 0.
- All 1s and 0s of the string are distributed among all the groups.
The task is to distribute all the characters in such a way that minimum number of characters in any group is maximized and print that number of characters (i.e. minimum number of characters in any group among the K groups).
Examples:
Input: S = “01011”, K=5
Output: 1
Explanation: the K groups would be – {0}, {0}, {1}, {1}, {1}
Input: S = “11110000000111111”, K=4
Output: 3
Approach: The problem can be easily solved by greedy technique.
We will iteratively take each case into consideration i.e.
- for each i, from 1 to K-1,
- we will assign i groups to store all the zeroes and
- K-i groups to store all the ones.
- At each iteration, we will keep taking maximum of the current answer.
The following approach can be followed to arrive at the answer:
- Calculate the number of 0s and 1s in the string.
- Initialize answer by 0.
- If K>N (where N is length of string), return 0, as in this case it is not possible to make K groups such that each of them contains atleast one character.
- Iterate from i=1 to i=K-1. At each iteration, assign i groups to store all the zeroes and K-i groups to store all the ones.
- Find number of zeroes in each of the i groups by dividing total number of 0s by the number of groups assigned to store the 0s (suppose some zeroes are left, as they may not be distributed equally to all the groups, then they can be put into any one group. It doesn’t matter, as we need the group with minimum characters). Similarly, find number of 1s in each group.
- Find minimum of both (i.e. 0s in a group and 1s in a group) and store in a variable, say min_count.
- Find maximum of all min_count in each iteration and return that.
Below is the code based on above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int divideString(string S, int K)
{
int N = S.length();
int zero = 0, one = 0;
for ( int i = 0; i < N; i++) {
if (S[i] == '0' ) {
zero++;
}
else {
one++;
}
}
int answer = 0;
if (K > N) {
return 0;
}
for ( int i = 1; i < K; i++) {
int zero_groups = i;
int one_groups = K - i;
int count0 = zero / zero_groups;
int count1 = one / one_groups;
int min_count = min(count0, count1);
answer = max(answer, min_count);
}
return answer;
}
int main()
{
string S = "11110000000111111" ;
int K = 4;
int ans = divideString(S, K);
cout << ans;
}
|
Java
import java.io.*;
class GFG {
static int divideString(String S, int K)
{
int N = S.length();
int zero = 0 , one = 0 ;
for ( int i = 0 ; i < N; i++) {
if (S.charAt(i) == '0' ) {
zero++;
}
else {
one++;
}
}
int answer = 0 ;
if (K > N) {
return 0 ;
}
for ( int i = 1 ; i < K; i++) {
int zero_groups = i;
int one_groups = K - i;
int count0 = zero / zero_groups;
int count1 = one / one_groups;
int min_count = Math.min(count0, count1);
answer = Math.max(answer, min_count);
}
return answer;
}
public static void main (String[] args) {
String S = "11110000000111111" ;
int K = 4 ;
int ans = divideString(S, K);
System.out.println(ans);
}
}
|
Python3
def divideString(S, K):
N = len (S)
zero, one = 0 , 0
for i in range ( 0 , N):
if S[i] = = '0' :
zero + = 1
else :
one + = 1
answer = 0
if K > N:
return 0
for i in range ( 1 , K):
zero_groups = i
one_groups = K - i
count0 = zero / / zero_groups
count1 = one / / one_groups
min_count = min (count0, count1)
answer = max (answer, min_count)
return answer
S = '11110000000111111'
K = 4
ans = divideString(S, K)
print (ans)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int divideString( string S, int K)
{
int N = S.Length;
int zero = 0, one = 0;
for ( int i = 0; i < N; i++) {
if (S[i] == '0' ) {
zero++;
}
else {
one++;
}
}
int answer = 0;
if (K > N) {
return 0;
}
for ( int i = 1; i < K; i++) {
int zero_groups = i;
int one_groups = K - i;
int count0 = zero / zero_groups;
int count1 = one / one_groups;
int min_count = Math.Min(count0, count1);
answer = Math.Max(answer, min_count);
}
return answer;
}
public static void Main(String[] args)
{
string S = "11110000000111111" ;
int K = 4;
int ans = divideString(S, K);
Console.Write(ans);
}
}
|
Javascript
<script>
const divideString = (S, K) => {
let N = S.length;
let zero = 0, one = 0;
for (let i = 0; i < N; i++) {
if (S[i] == '0' ) {
zero++;
}
else {
one++;
}
}
let answer = 0;
if (K > N) {
return 0;
}
for (let i = 1; i < K; i++) {
let zero_groups = i;
let one_groups = K - i;
let count0 = parseInt(zero / zero_groups);
let count1 = parseInt(one / one_groups);
let min_count = Math.min(count0, count1);
answer = Math.max(answer, min_count);
}
return answer;
}
let S = "11110000000111111" ;
let K = 4;
let ans = divideString(S, K);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach:
- Count the number of ‘1’ and ‘0’ characters in the input string “S”.
- Determine the maximum count among the ‘1’s and ‘0’s.
- Calculate the minimum number of characters in any group by dividing the maximum count by “K” and rounding up to the nearest integer.
- Return the minimum number of characters as the output.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
#include <algorithm>
int distributeCharacters(std::string S, int K) {
int count_1 = std::count(S.begin(), S.end(), '1' );
int count_0 = std::count(S.begin(), S.end(), '0' );
int max_characters = std::max(count_1, count_0);
int min_characters = std::max(1, (max_characters + K - 1) / K);
return min_characters;
}
int main() {
std::string S = "11110000000111111" ;
int K = 4;
int result = distributeCharacters(S, K);
std::cout << result << std::endl;
return 0;
}
|
Java
public class GFG {
public static int distributeCharacters(String S, int K) {
int count_1 = S.split( "1" , - 1 ).length - 1 ;
int count_0 = S.split( "0" , - 1 ).length - 1 ;
int max_characters = Math.max(count_1, count_0);
int min_characters = Math.max( 1 , ( int ) Math.floor((max_characters + K - 1 ) / K));
return min_characters;
}
public static void main(String[] args) {
String S = "11110000000111111" ;
int K = 4 ;
int result = distributeCharacters(S, K);
System.out.println(result);
}
}
|
Python3
def distribute_characters(S, K):
count_1 = S.count( '1' )
count_0 = S.count( '0' )
max_characters = max (count_1, count_0)
min_characters = max ( 1 , (max_characters + K - 1 ) / / K)
return min_characters
def main():
S = "11110000000111111"
K = 4
result = distribute_characters(S, K)
print (result)
if __name__ = = '__main__' :
main()
|
C#
using System;
class GFG {
static int DistributeCharacters( string S, int K)
{
int count_1 = CountOccurrences(S, '1' );
int count_0 = CountOccurrences(S, '0' );
int max_characters = Math.Max(count_1, count_0);
int min_characters
= Math.Max(1, (max_characters + K - 1) / K);
return min_characters;
}
static int CountOccurrences( string str, char c)
{
int count = 0;
foreach ( char ch in str)
{
if (ch == c) {
count++;
}
}
return count;
}
static void Main( string [] args)
{
string S = "11110000000111111" ;
int K = 4;
int result = DistributeCharacters(S, K);
Console.WriteLine(result);
}
}
|
Javascript
function distributeCharacters(S, K) {
const count_1 = S.split( '1' ).length - 1;
const count_0 = S.split( '0' ).length - 1;
const max_characters = Math.max(count_1, count_0);
const min_characters = Math.max(1, Math.ceil(max_characters / K));
return min_characters;
}
const S = "11110000000111111" ;
const K = 4;
const result = distributeCharacters(S, K);
console.log(result);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...