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

In C programming, we need to find the count of "10" subsequences for each '1' in a string that contains A individual 1s, B "10" substrings, and C individual 0s. This problem involves counting subsequences where each '1' can pair with available '0' characters.

Syntax

long long maximumSubSeq(int A, int B, int C);

Problem Understanding

Given three parameters

  • A Number of standalone '1' characters
  • B Number of "10" substrings
  • C Number of standalone '0' characters

We need to count all possible "10" subsequences from the concatenated string.

Example 1: Basic Case

#include <stdio.h>

long long maximumSubSeq(int A, int B, int C) {
    long long mod = 1000000007;
    
    // Count subsequences from A*B (standalone 1s with 10s) + B*(B+1)/2 (10s with each other)
    long long res = (A * 1LL * B) % mod + ((B * 1LL * (B + 1)) / 2) % mod;
    if (res >= mod) {
        res -= mod;
    }
    
    // Add subsequences from (A+B) total 1s with C standalone 0s
    res += ((A + B) * 1LL * C) % mod;
    if (res >= mod) {
        res -= mod;
    }
    
    return res;
}

int main() {
    int A = 1, B = 2, C = 3;
    printf("Input: A=%d, B=%d, C=%d<br>", A, B, C);
    printf("Output: %lld<br>", maximumSubSeq(A, B, C));
    return 0;
}
Input: A=1, B=2, C=3
Output: 14

Example 2: Different Values

#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() {
    // Test multiple cases
    int testCases[][3] = {{2, 1, 0}, {4, 1, 1}, {0, 2, 3}};
    int numTests = 3;
    
    for (int i = 0; i < numTests; i++) {
        int A = testCases[i][0];
        int B = testCases[i][1]; 
        int C = testCases[i][2];
        printf("A=%d, B=%d, C=%d: %lld<br>", A, B, C, maximumSubSeq(A, B, C));
    }
    
    return 0;
}
A=2, B=1, C=0: 3
A=4, B=1, C=1: 10
A=0, B=2, C=3: 9

How It Works

The algorithm works in two main steps

  1. Count from A and B Each standalone '1' (A) can pair with '0' from "10" strings (B), plus each "10" string contributes its own subsequences
  2. Count from C All '1' characters (both standalone A and from B "10" strings) can pair with standalone '0' characters (C)

The formula is: A*B + B*(B+1)/2 + (A+B)*C

Key Points

  • Use long long to handle large numbers and prevent overflow
  • Apply modulo 10^9 + 7 to keep results within bounds
  • The 1LL ensures proper type casting for multiplication
  • Check for modulo reduction after each major calculation step

Conclusion

This C program efficiently counts "10" subsequences using mathematical formulas rather than string manipulation. The approach handles large inputs using modular arithmetic and provides O(1) time complexity.

Updated on: 2026-03-15T14:32:49+05:30

315 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements