Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when converted into integer are divisible by 4. Substring may contain leading zeroes.
Input : "124" Output : 4 Substrings divisible by 4 are "12", "4", "24", "124" . Input : "04" Output : 3 Substring divisible by 4 are "0", "4", "04" .
Efficient solution : A number is divisible by 4 if its last two digits are divisible by 4 and single-digit numbers divisible by 4 are 4, 8 and 0. So, to calculate the number of substrings divisible by 4 we first count number of 0’s, 4’s and 8’s in the string. Then, we make all pairs of two consecutive characters and convert it into an integer. After converting it into integer we check that whether it is divisible by 4 or not. If it is divisible by 4 then all such substring ending with this last two characters are divisible by 4. Now, the number of such substrings are basically the index of 1st character of pair. To make it more clear, consider string “14532465” then possible pairs are “14”, “45”, “53”, “32”, “24”, “46”, “65” . In these pairs only “32” and “24” when converted into integer are divisible by 4. Then, substrings ( length >= 2 ) divisible by 4 must end with either “32” or “24” So, number of substrings ending with “32” are “14532”, “4532”, “532”, “32” i.e 4 and index of ‘3’ is also 4 . Similarly, the number of substrings ending with “24” is 5.
Thus we get an O(n) solution. Below is the implementation of this approach .
This article is contributed by Surya Priy. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Number of substrings divisible by 6 in a string of integers
- Number of substrings divisible by 8 but not by 3
- Number of substrings with length divisible by the number of 1's in it
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Number of substrings of a string
- Number of even substrings in a string of digits
- Number of subsequences in a string divisible by n
- Number of sub-strings in a given binary string divisible by 2
- Maximum splits in binary string such that each substring is divisible by given odd number
- Sum of last digit of all integers from 1 to N divisible by M
- Count integers in the range [A, B] that are not divisible by C and D
- Count of m digit integers that are divisible by an integer n
- Count integers in a range which are divisible by their euler totient value
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Lexicographically all Shortest Palindromic Substrings from a given string
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Remove characters from a numeric string such that string becomes divisible by 8
- Number of substrings that start with "geeks" and end with "for"
- Number of balanced parenthesis substrings
- Count number of substrings with numeric value greater than X