Given a string of digits “0-9”. The task is find the number of substrings which are divisible by 8 but not by 3.
Input : str = "888" Output : 5 Substring indexes : (1, 1), (1, 2), (2, 2), (2, 3), (3, 3). Input : str = "6564525600" Output : 15
A number is divisible by 3 if sum of its digits is divisible by 3. And the number is divisible by 8 if last three digits are divisible by 8.
Now, the idea is to store the prefix sum of the string i.e count of prefixes such that sum of the digits of the prefix modulo 3 is either 0, 1, 2. Next we iterate over the string and for each position i, count the number of substrings ending at i and divisible by 8. From this value, we subtract the number of substrings ending at i and divisible by 3.
We define a |S| X 3 size 2D array, |S| is size of string, say dp[i][j].
dp[i][j] can be define as at any index i, number of substring starting from index 0 to index i having output j
when digits from index 0 are added upto index i and modulo 3. Therefore 0 <= j <= 2, since modulo 3.
Now, we will iterate over string and check each one digit number, two digit number and three digit number which are divisible by 8.
For one digit, just check if character at index is 8 or not.
For two digit, check whether it is divisible by 8 and not divisible by 3.
For three digit, form the number and check if it is divisible by 8 or not. If the number is divisible then last three digits must be divisible by 8. So all the substring ending at this index must be divisible by 8 i.e (i-3) substrings.
But it will also contain those substring which are divisible by 3, so simply remove them using dp[i][j].
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
- Number of substrings divisible by 4 in a string of integers
- Number of substrings divisible by 6 in a string of integers
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Number of substrings of a string
- Number of substrings with count of each character as k
- Number of even substrings in a string of digits
- Sum of all substrings of a string representing a number | Set 1
- Count Substrings with equal number of 0s, 1s and 2s
- Number of substrings of one string present in other
- Number of substrings with odd decimal value in a binary string
- Count number of substrings with exactly k distinct characters
- Count number of substrings with numeric value greater than X
- Count the number of vowels occurring in all the substrings of given string
- Given a binary string, count number of substrings that start and end with 1.
- Rearrange the string to maximize the number of palindromic substrings