Count of sum of “10” subsequences for each 1 in string with A 1s, B 10s and C 0s
Last Updated :
23 Mar, 2022
Given an A “1″s, B “10”s, and C “0”s, the task is to count of sum of “10” subsequences for each 1s in the string with A 1s, B 10s and C 0s
Examples:
Input: A = 1, B = 2, C = 3
Output: 14
Explanation: A = 1 means one times “1” string, B = 2 means two times “10” strings, and C = 3 means three times”0″ strings.
After Concatenation, the string formed is “11010000”.
So for 1st “1”, five “10” subsequences are possible,
for 2nd “1” five “10” subsequences are possible, and
for 3rd “1” four “10” subsequences are possible.
Hence total 5 + 5 + 4 = 14 subsequences are possible.
Input: A = 0, B = 0, C = 1
Output: 0
Explanation: A = 0 means no “1” string, B = 0 means no “10” string, and C = 1 means one times “0” string.
So, final string is “0” .
Hence no “10” subsequence is possible and therefore output is 0.
Naive Approach: The most simple solution for this problem is to generate the string, and then for each 1, find the count of “10” subsequences possible. Return the sum of count of all such subsequences at the end.
Time Complexity: O(N*countOf1s)
Auxiliary Space: O(1)
Efficient Approach: The idea to solve the problem efficiently using some basic concepts of mathematics and combinatorials.
- To get maximum subsequence, append all “1” at the start of final string and “0” at the end of final string.
- Declare ans variable and store count of possible “10” subsequence form by A times “1” and B times “10” by multiplying (A*B)+((B*(B+1))/2).
- Add ans with count of possible “10” subsequence form by C times “0” and A & B time “1” by multiplying (A*B)*C.
- Return Modulo of answer.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int maxsubsequence( int A, int B, int C)
{
long long mod = 1e9 + 7;
long long ans
= (A * 1l * B) % mod
+ ((B * 1l * (B + 1)) / 2) % mod;
if (ans >= mod) {
ans -= mod;
}
ans += ((A + B) * 1l * C) % mod;
if (ans >= mod) {
ans -= mod;
}
return ans;
}
int main()
{
int A = 1, B = 2, C = 3;
cout << maxsubsequence(A, B, C) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int maxsubsequence( int A, int B, int C)
{
long mod = ( long )(1e9 + 7 );
long ans = ( long )(A * B) % mod
+ ((B * (B + 1 )) / 2 ) % mod;
if (ans >= mod) {
ans -= mod;
}
ans += ((A + B) * C) % mod;
if (ans >= mod) {
ans -= mod;
}
return ( int )ans;
}
public static void main(String[] args)
{
int A = 1 , B = 2 , C = 3 ;
System.out.println(maxsubsequence(A, B, C));
}
}
|
Python3
def maxsubsequence(A, B, C):
mod = int ( 1e9 + 7 )
ans = (A * 1 * B) % mod + ((B * 1 * (B + 1 )) / / 2 ) % mod
if (ans > = mod):
ans - = mod
ans + = ((A + B) * 1 * C) % mod
if (ans > = mod):
ans - = mod
return ans
if __name__ = = "__main__" :
A, B, C = 1 , 2 , 3
print (maxsubsequence(A, B, C))
|
C#
using System;
class GFG{
static int maxsubsequence( int A, int B, int C)
{
long mod = ( long )(1e9 + 7);
long ans = ( long )(A * B) % mod
+ ((B * (B + 1)) / 2) % mod;
if (ans >= mod) {
ans -= mod;
}
ans += ((A + B) * C) % mod;
if (ans >= mod) {
ans -= mod;
}
return ( int )ans;
}
static public void Main (){
int A = 1, B = 2, C = 3;
Console.Write(maxsubsequence(A, B, C));
}
}
|
Javascript
<script>
function maxsubsequence( A, B, C)
{
let mod = 1e9 + 7;
let ans
= (A * 1 * B) % mod
+ ((B * 1* (B + 1)) / 2) % mod;
if (ans >= mod) {
ans -= mod;
}
ans += ((A + B) * 1 * C) % mod;
if (ans >= mod) {
ans -= mod;
}
return ans;
}
let A = 1, B = 2, C = 3;
document.write(maxsubsequence(A, B, C) + '<br>' );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...