Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • 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++




// C++ Code for the above approach:
 
#include <iostream>
using namespace std;
int maxsubsequence(int A, int B, int C)
{
 
    // As the answer may be very large,
    // Find it modulo 109 + 7
    long long mod = 1e9 + 7;
 
    // Count possible subsequence by
    // A times"1" and B times"10"
    long long ans
        = (A * 1l * B) % mod
          + ((B * 1l * (B + 1)) / 2) % mod;
    if (ans >= mod) {
        ans -= mod;
    }
 
    // Count possible subsequence
    // By C times "0" and A & B time  "1"
    ans += ((A + B) * 1l * C) % mod;
    if (ans >= mod) {
        ans -= mod;
    }
    return ans;
}
 
// Driver code
int main()
{
    int A = 1, B = 2, C = 3;
    cout << maxsubsequence(A, B, C) << endl;
    return 0;
}


Java




// JAVA Code for the above approach:
 
import java.util.*;
class GFG {
  public static int maxsubsequence(int A, int B, int C)
  {
 
    // As the answer may be very large,
    // Find it modulo 109 + 7
    long mod = (long)(1e9 + 7);
 
    // Count possible subsequence by
    // A times"1" and B times"10"
    long ans = (long)(A * B) % mod
      + ((B * (B + 1)) / 2) % mod;
    if (ans >= mod) {
      ans -= mod;
    }
 
    // Count possible subsequence
    // By C times "0" and A & B time  "1"
    ans += ((A + B) * C) % mod;
    if (ans >= mod) {
      ans -= mod;
    }
    return (int)ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int A = 1, B = 2, C = 3;
    System.out.println(maxsubsequence(A, B, C));
  }
}
 
// This code is contributed by Taranpreet


Python3




# python3 Code for the above approach:
def maxsubsequence(A, B, C):
 
    # As the answer may be very large,
    # Find it modulo 109 + 7
    mod = int(1e9 + 7)
 
    # Count possible subsequence by
    # A times"1" and B times"10"
    ans = (A * 1 * B) % mod + ((B * 1 * (B + 1)) // 2) % mod
    if (ans >= mod):
        ans -= mod
 
    # Count possible subsequence
    # By C times "0" and A & B time "1"
    ans += ((A + B) * 1 * C) % mod
    if (ans >= mod):
        ans -= mod
 
    return ans
 
# Driver code
if __name__ == "__main__":
 
    A, B, C = 1, 2, 3
    print(maxsubsequence(A, B, C))
 
# This code is contributed by rakeshsahni


C#




// C# Code for the above approach:
using System;
class GFG{
 
  static int maxsubsequence(int A, int B, int C)
  {
 
    // As the answer may be very large,
    // Find it modulo 109 + 7
    long mod = (long)(1e9 + 7);
 
    // Count possible subsequence by
    // A times"1" and B times"10"
    long ans = (long)(A * B) % mod
      + ((B * (B + 1)) / 2) % mod;
    if (ans >= mod) {
      ans -= mod;
    }
 
    // Count possible subsequence
    // By C times "0" and A & B time  "1"
    ans += ((A + B) * C) % mod;
    if (ans >= mod) {
      ans -= mod;
    }
    return (int)ans;
  }
 
  // Driver code
  static public void Main (){
 
    int A = 1, B = 2, C = 3;
    Console.Write(maxsubsequence(A, B, C));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
        // JavaScript code for the above approach
function maxsubsequence( A, B, C)
{
 
    // As the answer may be very large,
    // Find it modulo 109 + 7
    let mod = 1e9 + 7;
 
    // Count possible subsequence by
    // A times"1" and B times"10"
    let ans
        = (A * 1 * B) % mod
          + ((B * 1* (B + 1)) / 2) % mod;
    if (ans >= mod) {
        ans -= mod;
    }
 
    // Count possible subsequence
    // By C times "0" and A & B time  "1"
    ans += ((A + B) * 1 * C) % mod;
    if (ans >= mod) {
        ans -= mod;
    }
    return ans;
}
 
// Driver code
 
    let A = 1, B = 2, C = 3;
    document.write(maxsubsequence(A, B, C) + '<br>');
   
     // This code is contributed by Potta Lokesh
    </script>


 
 

Output

14

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!