Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:23:38+00:00 2026-06-09T02:23:38+00:00

I have a string whose size can be as large as 10,000. I have

  • 0

I have a string whose size can be as large as “10,000”. I have to count those SUBSEQUENCES which are divisible by 9.

SUBSEQUENCE: A subsequence is an arrangement in which the order of characters of given string is maintained. For ex: if given string is 10292 then some of its subsequences are 1, 102, 10, 19, 12, 12(12 is twice as 2 comes twice), 129, 029, 09, 092, etc. Some numbers which are not subsequences of given string are: 201(2 and 0 can’t come before 1), 921, 0291, etc.

I have tried to generate all subsequences(powerset) of given string using bit shifting and checking each string if it is divisible by 9. But this works fine as long as length of string is <=10. After that, I don’t get proper subsequences(some subsequences are displayed negative numbers).

Below is my code:

    scanf("%s", &str); //input string 

    int n=strlen(str); //find length of string

    //loop to generate subsequences
    for(i=1;i<(1<<n);++i){

        string subseq;

        for(j=0;j<n;++j){

            if(i&(1<<j)){

                subseq+=str[j]; // generate subsequence
            }
        }

        //convert generated subseq to int; number is 'long' tpye
        number=atol(subseq.c_str());printf("%ld\n", number); 

        //ignore 0 and check if number divisible by 9
        if(number!=0&&number%9==0)count++;
    }

        printf("%ld\n", count);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:23:40+00:00Added an answer on June 9, 2026 at 2:23 am

    Since a number is divisible by nine if and only if the sum of its digits is divisible by nine, you can get away with this problem with a O(n) recursive algorithm.

    The idea is the following: at each step, split in two the subsequence and determine (recursively) how many sequences have the sum of its digits be i % 9, where i ranges from 0 to 8. Then, you build up this very same table for the whole range by “merging” the two tables in O(1) in the following way. Let’s say L is the table for the left split and R for the right one and you need to build the table F for the whole range.

    Then you have:

    for (i = 0; i < 9; i++) {
      F[i] = L[i] + R[i];
      for (j = 0; j < 9; j++) {
        if (j <= i)
          F[i] += L[j] * R[i - j]
        else
          F[i] += L[j] * R[9 + i - j]
      }
    }
    

    The base case for a subsequence of only one digit d is obvious: just set F[d % 9] = 1 and all the other entries to zero.

    A full C++11 implementation:

    #include <iostream>
    #include <array>
    #include <tuple>
    #include <string>
    
    typedef std::array<unsigned int, 9> table;
    
    using std::tuple;
    using std::string;
    
    table count(string::iterator beg, string::iterator end)
    {
        table F;
        std::fill(F.begin(), F.end(), 0);
        if (beg == end)
            return F;
        if (beg + 1 == end) {
            F[(*beg - '0') % 9] = 1;
            return F;
        }
        size_t distance = std::distance(beg, end);
        string::iterator mid = beg + (distance / 2);
        table L = count(beg, mid);
        table R = count(mid, end);
    
        for (unsigned int i = 0; i < 9; i++) {
            F[i] = L[i] + R[i];
            for(unsigned int j = 0; j < 9; j++) {
                if (j <= i)
                    F[i] += L[j] * R[i - j];
                else
                    F[i] += L[j] * R[9 + i - j];
            }
        }
        return F;
    }
    
    table count(std::string s)
    {
        return count(s.begin(), s.end());
    }
    
    int main(void)
    {
        using std::cout;
        using std::endl;
        cout << count("1234")[0] << endl;
        cout << count("12349")[0] << endl;
        cout << count("9999")[0] << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
I have a class whose constructor takes a const reference to a string. This
I have an input field whose name is an MD5 string e.g.: <input type=hidden
I have a large string in a variable that includes a whole bunch of
I have a large story in String format. I want to show the text
Can I use an Array of Dictionary objects? I have an XML which I
I have a style applied to my whole application: AndroidManifest.xml: <application android:theme=@style/ApplicationStyle android:icon=@drawable/icon android:label=@string/app_name>
I have string that look like Array that fetched from other webservice like this
I have string that displays UTF-8 encoded characters, and I want to convert it
I have string as abvd.qweqw.sdfs.a=aqwrwewrwerrew . I need to parse this string and get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.