Find the minimum number of distinct Substrings formed by 0s in Binary String
Last Updated :
05 Dec, 2023
Given two integers N and M, the task is to output the minimum number of different substrings that can be formed using the Binary String of length N having M number of set bits.
Note: Two substrings let say S[x, y] and S[a, b] are different. If either x != a or y != b.
Examples:
Input: N = 3, M = 1
Output: 2
Explanation: There can be 3 possible binary strings of length 3 having 1 set bit. Let us see all of them one by one.
- First Binary String: S = 100. Which has three substrings formed by only 0s, which are S[1, 1], S[2, 2], and S[1, 2] as "0", "0" and "00" respectively. Total number of substrings is 3.
- Second Binary String: S = 010. Which has two substrings formed by only 0s, which are S[0, 0], S[2, 2] "0" and "0" respectively. The total number of substrings is 2.
- Third Binary String: S = 001. Which has three substrings formed by only 0s, which are S[0, 0], S[1, 1], and S[0, 1] as "0", "0" and "00" respectively. The total number of substrings is 3.
Input: N = 5, M = 2
Output: 3
Explanation: It can be verified that there will be only 3 substrings, which are the minimum possible, according to the problem statement. In this case, The length of the binary string is 5 and there must be 2 occurrences of the 1. The possible binary strings are “11000”, “10100”, “10010”, “10001”, “01100”, “01010”, “01001”, “00110”, “00101”, and “00011”. Among these, the string “01010” has the minimum number of all-zero substrings, which is 3. Therefore, the output is 3.
Approach: Implement the idea below to solve the problem
The problem is observation based. The key idea of the problem is to minimize the number of all-zero substrings in a binary string of length N with M occurrences of the 1. The approach is based on the combinatorics and distribution. The crux is to distribute the M ones in the N-M zeros such that the number of substrings with all zeros is minimized. This is achieved by placing as many zeros as possible between each pair of ones.
Here's how it works:
- Calculate x as (N-M)/(M+1). This represents the number of zeros that can be placed between each pair of ones to minimize the number of all-zero substrings. Essentially, we are distributing the zeros evenly among the ones.
- Calculate y as (N-M)%(M+1). This represents the remaining zeros after distributing x zeros between each pair of ones. These remaining zeros are then distributed among the ones starting from the left.
- Calculate ans as (M*x+x+2*y)*(x+1)/2. Which is the number of different substrings of string S, made up of the character 0 only.
This approach ensures that the ones are spread out as much as possible, thereby minimizing the number of all-zero substrings.
Steps were taken to solve the problem:
- Create a variable let say X and initialize as (N-M)/(M+1). This represents the number of zeros that can be placed between each pair of ones to minimize the number of all-zero substrings.
- Create a variable let say Y and initialize as (N-M)%(M+1). This represents the remaining zeros after distributing X zeros between each pair of ones.
- Calculate Ans as (M*X+X+2*Y)*(X+1)/2. This represents the minimum number of substrings formed by 0s.
- Output the value of Ans.
Code to implement the approach:
C++
// C++ Implementation
#include <iostream>
void Minimum_subStrings(long N, long M) {
// Implementation of discussed approach
long X = (N - M) / (M + 1);
long Y = (N - M) % (M + 1);
long ans = (M * X + X + 2 * Y) * (X + 1);
ans = ans / 2;
// Printing the minimum possible
// Substrings
std::cout << ans << std::endl;
}
int main() {
// Inputs
long N = 5;
long M = 2;
// Function call
Minimum_subStrings(N, M);
return 0;
}
// This code is contributed by Sakshi
Java
// Java code to implement the approach
import java.util.*;
// Driver Class
class Main {
// Driver Function
public static void main(String[] args)
throws java.lang.Exception
{
// Inputs
long N = 5;
long M = 2;
// Function call
Minimum_subStrings(N, M);
}
public static void Minimum_subStrings(long N, long M)
{
// Implementation of discussed approach
long X = (N - M) / (M + 1);
long Y = (N - M) % (M + 1);
long ans = (M * X + X + 2 * Y) * (X + 1);
ans = ans / 2;
// Printing the minimum possible
// Substrings
System.out.println(ans);
}
}
Python3
# Python code for the above approach
def Minimum_subStrings(N, M):
# Implementation of discussed approach
X = (N - M) // (M + 1)
Y = (N - M) % (M + 1)
ans = (M * X + X + 2 * Y) * (X + 1)
ans = ans // 2
# Printing the minimum possible
# Substrings
print(ans)
# Driver code
def main():
# Inputs
N = 5
M = 2
# Function call
Minimum_subStrings(N, M)
if __name__ == '__main__':
main()
# This code is contributed by ragul21
C#
using System;
class GFG
{
static void MinimumSubstrings(long N, long M)
{
// Implementation of the discussed approach
long X = (N - M) / (M + 1);
long Y = (N - M) % (M + 1);
long ans = (M * X + X + 2 * Y) * (X + 1) / 2;
// Printing the minimum possible
// Substrings
Console.WriteLine(ans);
}
static void Main()
{
// Inputs
long N = 5;
long M = 2;
// Function call
MinimumSubstrings(N, M);
}
}
JavaScript
// JavaScript code to implement the approach
// Driver Code
// Inputs
const N = 5;
const M = 2;
// Function call
minimumSubStrings(N, M);
function minimumSubStrings(N, M) {
// Implementation of discussed approach
const X = Math.floor((N - M) / (M + 1));
const Y = (N - M) % (M + 1);
let ans = (M * X + X + 2 * Y) * (X + 1);
ans = Math.floor(ans / 2);
// Printing the minimum possible
// Substrings
console.log(ans);
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Number of sub-strings in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of substrings of str which are divisible by 2. Leading zeros in a substring are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only substrings which are divisible by 2. Input: str = "10010" Output: 10 Naive appr
4 min read
Minimum number whose binary form is not a subsequence of given binary string Given a binary string S of size N, the task is to find the minimum non-negative integer which is not a subsequence of the given string S in its binary form. Examples: Input: S = "0000"Output:1Explanation: 1 whose binary representation is "1" is the smallest non-negative integer which is not a subseq
8 min read
Longest substring of 0s in a string formed by k concatenations Given a binary string of length n and an integer k. Consider another string T which is formed by concatenating the given binary string k times. The task is to print the maximum size of a substring of T containing only zeroes. Examples: Input: str = 110010, k = 3 Output: 2 str = 110010 T = 1100101100
8 min read
Number of subsequences in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of subsequences of str which are divisible by 2. Leading zeros in a sub-sequence are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only subsequences which are divisible by 2.Input: str = "10010" Output: 22 Naiv
4 min read
Number of substrings with odd decimal value in a binary string Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Subst
6 min read
Count of substrings in a Binary String that contains more 1s than 0s Given a binary string s, the task is to calculate the number of such substrings where the count of 1's is strictly greater than the count of 0's. Examples Input: S = "110011"Output: 11Explanation: Substrings in which the count of 1's is strictly greater than the count of 0's are { S[0]}, {S[0], S[1]
15+ min read
Number of binary strings such that there is no substring of length ⥠3 Given an integer N, the task is to count the number of binary strings possible such that there is no substring of length ? 3 of all 1's. This count can become very large so print the answer modulo 109 + 7.Examples: Input: N = 4 Output: 13 All possible valid strings are 0000, 0001, 0010, 0100, 1000,
10 min read
Split the binary string into substrings with equal number of 0s and 1s Given a binary string str of length N, the task is to find the maximum count of consecutive substrings str can be divided into such that all the substrings are balanced i.e. they have equal number of 0s and 1s. If it is not possible to split str satisfying the conditions then print -1.Example: Input
8 min read
Minimum steps to remove substring 010 from a binary string Given a binary string, the task is to count the minimum steps to remove substring "010" from this binary string. Examples: Input: binary_string = "0101010" Output: 2 Switching 0 to 1 at index 2 and index 4 will remove the substring 010. Hence the number of steps needed is 2. Input: binary_string = "
4 min read
Minimize the maximum of 0s left or 1s deleted from Binary String You are given a binary string S of size N consisting of characters 0 and/or 1. You may remove several characters from the beginning or the end of the string. The task is to find the cost of removal where the cost is the maximum of the following two values: The number of characters 0 left in the stri
8 min read