Open In App

Binary String Partitioning

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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() {
    // Input value of N
    int n = 6;
 
    // Input value of K
    int k = 2;
 
    // Input value of D
    int d = 2;
 
    // Input string
    string str = "110011";
 
    // Variable to store answer
    int ans = 0;
 
    // Loop for traversing the characters in the string
    for (int i = 0; i < n; i++) {
        if (str[i] == '1')
            ans++;
        else
            ans--;
    }
 
    // Calculate the minimum number of operations
    if (ans < 0)
        ans = ans * (-1);
    int min = ans / k;
    if (ans % k != 0)
        min++;
 
    // Printing output
    cout << (min < d ? "YES" : "NO") << endl;
 
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Driver Function
    public static void main(String[] args)
        throws java.lang.Exception
    {
 
        // Input value of N
        int n = 6;
 
        // Input value of K
        int k = 2;
 
        // Input value of D
        int d = 2;
 
        // Input string
        String str = "110011";
 
        // Variable to store answer
        int ans = 0;
 
        // Char array holding characters of string
        char[] c = str.toCharArray();
 
        // Loop for traversing on char array
        for (int i = 0; i < n; i++) {
            if (c[i] == '1')
                ans++;
            else
                ans--;
        }
 
        // Printing output
        if (ans < 0)
            ans = ans * (-1);
        int min = ans / k;
        if (ans % k != 0)
            min++;
        System.out.println(min < d ? "YES" : "NO");
    }
}


Python3




# Python Code
 
# Input value of N
n = 6
 
# Input value of K
k = 2
 
# Input value of D
d = 2
 
# Input string
str_input = "110011"
 
# Variable to store answer
ans = 0
 
# Loop for traversing the characters in the string
for i in range(n):
    if str_input[i] == '1':
        ans += 1
    else:
        ans -= 1
 
# Calculate the minimum number of operations
ans = abs(ans)
min_operations = ans // k + (1 if ans % k != 0 else 0)
 
# Printing output
print("YES" if min_operations < d else "NO")
 
 
# This code is contributed by guptapratik


C#




using System;
class GFG {
    // Driver Function
    public static void Main(string[] args)
    {
        // Input value of N
        int n = 6;
 
        // Input value of K
        int k = 2;
 
        // Input value of D
        int d = 2;
 
        // Input string
        string str = "110011";
 
        // Variable to store answer
        int ans = 0;
 
        // Char array holding characters of string
        char[] c = str.ToCharArray();
 
        // Loop for traversing on char array
        for (int i = 0; i < n; i++) {
            if (c[i] == '1')
                ans++;
            else
                ans--;
        }
 
        // Printing output
        if (ans < 0)
            ans = ans * (-1);
        int min = ans / k;
        if (ans % k != 0)
            min++;
        Console.WriteLine(min < d ? "YES" : "NO");
    }
}


Javascript




//Javascript Code
 
// Input value of N
let n = 6;
 
// Input value of K
let k = 2;
 
// Input value of D
let d = 2;
 
// Input string
let str = "110011";
 
// Variable to store answer
let ans = 0;
 
// Loop for traversing the characters in the string
for (let i = 0; i < n; i++) {
    if (str[i] === '1')
        ans++;
    else
        ans--;
}
 
// Calculate the minimum number of operations
if (ans < 0)
    ans = ans * (-1);
let min = Math.floor(ans / k);
if (ans % k !== 0)
    min++;
 
// Printing output
console.log(min < d ? "YES" : "NO");


Output

YES










Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads