Given a number as a string, write a function to find the number of substrings (or contiguous subsequences) of the given string which recursively add up to 9.
For example digits of 729 recursively add to 9,
7 + 2 + 9 = 18
Recur for 18
1 + 8 = 9
Input: 4189 Output: 3 There are three substrings which recursively add to 9. The substrings are 18, 9 and 189. Input: 909 Output: 5 There are 5 substrings which recursively add to nine, 9, 90, 909, 09, 9
This article is about an optimized solution of problem stated below article :
Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 1.
All digits of a number recursively add up to 9, if only if the number is multiple of 9. We basically need to check for s%9 for all substrings s. One trick used in below program is to do modular arithmetic to avoid overflow for big strings.
Initialize an array d of size 10 with 0 d<-1 Initialize mod_sum = 0, continuous_zero = 0 for every character if character == '0'; continuous_zero++ else continuous_zero=0 compute mod_sum update result += d[mod_sum] update d[mod_sum]++ subtract those cases from result which have only 0s
If sum of digits from index i to j add up to 9, then sum(0 to i-1) = sum(0 to j) (mod 9).
We just have to remove cases which contain only zeroes.We can do this by remembring the no. of continuous zeroes upto this character(no. of these cases ending on this index) and subtracting them from the result.
Following is a simple implementation based on this approach.
The implementation assumes that there are can be leading 0’s in the input number.
8 5 3
Time Complexity of the above program is O(n). Program also supports leading zeroes.
This article is contributed by Gulab Arora. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Check if a string can become empty by recursively deleting a given sub-string
- Count the number of contiguous increasing and decreasing subsequences in a sequence
- Count subsequences in first string which are anagrams of the second string
- Decode a string recursively encoded as count followed by substring
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Construct a string that has exactly K subsequences from given string
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Recursively remove all adjacent duplicates
- Recursively print all sentences that can be formed from list of word lists
- Count occurrences of a substring recursively
- Number of subsequences in a given binary string divisible by 2
- Count of subsequences which consists exactly K prime numbers
- Count subsequences which contains both the maximum and minimum array element
- Generating distinct subsequences of a given string in lexicographic order
- Count of 'GFG' Subsequences in the given string
- Lexicographically smallest permutation of a string with given subsequences
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Maximum contiguous 1 possible in a binary string after k rotations