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++ 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 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);
}
} |
# 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 |
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 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);
} |
3
Time Complexity: O(1)
Auxiliary Space: O(1)