Maximize count of strings of length 3 that can be formed from N 1s and M 0s
Given two numbers N and M which denotes the count of ones and zeros respectively, the task is to maximize the count of binary strings of length 3, consisting of both 0 and 1 in them, that can be formed from the given N 1s and M 0s.
Examples:
Input: N = 4, M = 5
Output: 3
Explanation:
Possible strings = { “001”, “011”, “001” }
Input: N = 818, M = 234
Output: 234
Naive Approach: Binary strings of three lengths can be formed as per the below conditions:
- If N > M: If N > 2, then reduce N by 2, M by 1, and since a string of type 110 is generated, thus increase the count by 1.
- If N ? M: If M > 2, then reduce M by 2, N by 1, and since a string of type 001 is generated, thus increase the count by 1.
Therefore, the idea is to iterate a loop until N or M becomes zero and keep updating the count of the string according to the above conditions.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void number_of_strings( int N, int M)
{
int ans = 0;
while (N > 0 && M > 0) {
if (N > M) {
if (N >= 2) {
N -= 2;
--M;
++ans;
}
else {
break ;
}
}
else {
if (M >= 2) {
M -= 2;
--N;
++ans;
}
else {
break ;
}
}
}
cout << ans;
}
int main()
{
int N = 4, M = 19;
number_of_strings(N, M);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static void number_of_strings( int N, int M)
{
int ans = 0 ;
while (N > 0 && M > 0 )
{
if (N > M)
{
if (N >= 2 )
{
N -= 2 ;
--M;
++ans;
}
else
{
break ;
}
}
else
{
if (M >= 2 )
{
M -= 2 ;
--N;
++ans;
}
else
{
break ;
}
}
}
System.out.println(ans);
}
public static void main (String[] args)
{
int N = 4 , M = 19 ;
number_of_strings(N, M);
}
}
|
Python3
def number_of_strings(N, M):
ans = 0
while (N > 0 and M > 0 ):
if (N > M):
if (N > = 2 ):
N - = 2
M - = 1
ans + = 1
else :
break
else :
if M > = 2 :
M - = 2
N - = 1
ans + = 1
else :
break
print (ans)
if __name__ = = '__main__' :
N = 4
M = 19
number_of_strings(N, M)
|
C#
using System;
class GFG{
static void number_of_strings( int N, int M)
{
int ans = 0;
while (N > 0 && M > 0)
{
if (N > M)
{
if (N >= 2)
{
N -= 2;
--M;
++ans;
}
else
{
break ;
}
}
else
{
if (M >= 2)
{
M -= 2;
--N;
++ans;
}
else
{
break ;
}
}
}
Console.WriteLine(ans);
}
public static void Main (String[] args)
{
int N = 4, M = 19;
number_of_strings(N, M);
}
}
|
Javascript
<script>
function number_of_strings(N , M)
{
var ans = 0;
while (N > 0 && M > 0)
{
if (N > M)
{
if (N >= 2)
{
N -= 2;
--M;
++ans;
}
else
{
break ;
}
}
else
{
if (M >= 2)
{
M -= 2;
--N;
++ans;
}
else
{
break ;
}
}
}
document.write(ans);
}
var N = 4, M = 19;
number_of_strings(N, M);
</script>
|
Time Complexity: O(max(A, B))
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, observe that the total number of binary strings that can be formed will be a minimum of N, M, and (N + M)/3 as:
- If N is minimum, and we have M ? 2*N then all the string of type 110 can be made.
- If M is minimum, and we have N ? 2*M then all the string of type 001 can be made.
- Else the total count will be (N + M)/3 and strings of both types 110 and 001 can be made.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void number_of_strings( int N, int M)
{
cout << min(N, min(M, (N + M) / 3));
}
int main()
{
int N = 4, M = 19;
number_of_strings(N, M);
return 0;
}
|
Java
class GFG{
static void number_of_Strings( int N, int M)
{
System.out.print(Math.min(N,
Math.min(M,
(N + M) /
3 )));
}
public static void main(String[] args)
{
int N = 4 , M = 19 ;
number_of_Strings(N, M);
}
}
|
Python3
def number_of_strings(N, M):
print ( min (N, min (M, (N + M) / / 3 )))
if __name__ = = '__main__' :
N = 4
M = 19
number_of_strings(N, M)
|
C#
using System;
class GFG{
static void number_of_Strings( int N,
int M)
{
Console.Write(Math.Min(N,
Math.Min(M, (N + M) / 3)));
}
public static void Main(String[] args)
{
int N = 4, M = 19;
number_of_Strings(N, M);
}
}
|
Javascript
<script>
function number_of_Strings(N , M)
{
document.write(Math.min(N,
Math.min(M,
(N + M) /
3)));
}
var N = 4, M = 19;
number_of_Strings(N, M);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
29 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...