Count of sum of β€œ10” subsequences for each 1 in string with A 1s, B 10s and C 0s


The aim of this article is to implement a program to obtain the count of sum of β€œ10” subsequences for each 1 in string with A 1s, B 10s and C 0s.

Example

Let us take the Input: A = 1, B = 2, C = 3
Output obtained here is : 14

Explanation

A = 1 denotes. There is a single "1" string, B = 2 denotes, there is a pair of "10" strings, and C = 3 denotes, there is a trio of "0" strings.

The string that results from concatenation is "11010000".

Five "10" subsequences are thus feasible for the first "1," five "10" subsequences for the second "1," and four "10" subsequences for the third "1".

There are therefore a total of 5 + 5 + 4 = 14 potential sequences.

Example

Let us take the Input: A = 2, B = 1, C =0
Output obtained here is: 3

Explanation

A = 2 denotes, there are two "1" strings, B = 1 denotes, there is a single "10" string, and C = 0 denotes there are no "0" strings.

The string that results from concatenation is "1110"

Therefore, the possible potential subsequences = 3.

Example

Let us take the Input: A = 4, B = 1, C =1
Output obtained here is : 10

Explanation

A = 4 denotes, there are four "1" strings, B = 1 denotes, there is a single "10" string, and C=1 denotes there is a single β€œ0" string.

The string that results from concatenation is "1111100"

Therefore, the possible potential subsequences = 10.

Example

Let us take the Input: A = 0, B = 2, C =3
Output obtained here is: 9

Explanation

A = 0 denotes, there are no "1" strings, B = 2 denotes, there is a pair of "10" strings, and C = 3 denotes, there are three "0" strings.

The string that results from concatenation is "1010000"

Therefore, the possible potential subsequences = 9.

Problem Statement

Implement a program to obtain the count of sum of β€œ10” subsequences for each 1 in string with A 1s, B 10s and C 0s

Approach

Append all "1"s at the beginning of the final string and all "0s" at the end of the final string to acquire the maximum subsequence.

Declare res as a variable, and then multiply A times "1" and B times "10" to store the count of potential "10" subsequences.

Add results that have a count of at least "10" subsequences by multiplying (A*B)*C times "0" and A & B times "1".

Return the result's modulo.

Algorithm

The algorithm to obtain the count of sum of β€œ10” subsequences for each 1 in string with A 1s, B 10s and C 0s given below βˆ’

  • Step 1 βˆ’ Define a function to obtain the count of sum.

  • Step 2 βˆ’ Because the result can be quite big, Calculate it modulo 109 + 7.

  • Step 3 βˆ’ Count the feasible subsequence by the following formula, res = (A * 1ll * B) % mod + ((B * 1ll * (B + 1)) / 2) % mod;

  • Step 4 βˆ’ Check whether the res is greater than or equals to mod, if so subtract mod from res.

  • Step 5 βˆ’ Count the feasible subsequence by the following formula, res += ((A + B) * 1ll * C) % mod;

  • Step 6 βˆ’ Again check whether the res is greater than or equals to mod, if so subtract mod from res.

  • Step 7 βˆ’ Print the output.

Example (C Program)

Here is the C program implementation of the above written algorithm to obtain the count of sum of β€œ10” subsequences for each 1 in string with A 1s, B 10s and C 0s.

#include <stdio.h>
long long maximumSubSeq(int A, int B, int C) {
   long long mod = 1000000007;
   long long res = (A * 1ll * B) % mod + ((B * 1ll * (B + 1)) / 2) % mod;
   if (res >= mod) {
      res -= mod;
   }
   res += ((A + B) * 1ll * C) % mod;
   if (res >= mod) {
      res -= mod;
   }
   return res;
}
int main(){
   int A = 2, B = 1, C = 0;
   printf("%lld
", maximumSubSeq(A, B, C)); return 0; }

Output

On execution, it will produce the following output βˆ’

3

Conclusion

Likewise, we can obtain the count of sum of β€œ10” subsequences for each 1 in string with A 1s, B 10s and C 0s. The challenge of obtaining the program to obtain the count of sum of β€œ10” subsequences for each 1 in string with A 1s, B 10s and C 0s is resolved in this article.

Here C programming code as well as the algorithm and the methodology to implement the program to obtain the count of sum of β€œ10” subsequences for each 1 in string with A 1s, B 10s and C 0s are provided.

Updated on: 31-Oct-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements