Given a large positive number as string, count all rotations of the given number which are divisible by 4.
Input: 8 Output: 1 Input: 20 Output: 1 Rotation: 20 is divisible by 4 02 is not divisible by 4 Input : 13502 Output : 0 No rotation is divisible by 4 Input : 43292816 Output : 5 5 rotations are : 43292816, 16432928, 81643292 92816432, 32928164
For large numbers it is difficult to rotate and divide each number by 4. Therefore, ‘divisibility by 4’ property is used which says that a number is divisible by 4 if the last 2 digits of the number is divisible by 4. Here we do not actually rotate the number and check last 2 digits for divisibility, instead we count consecutive pairs (in circular way) which are divisible by 4.
Consider a number 928160 Its rotations are 928160, 092816, 609281, 160928, 816092, 281609. Now form pairs from the original number 928160 as mentioned in the approach. Pairs: (9,2), (2,8), (8,1), (1,6), (6,0), (0,9) We can observe that the 2-digit number formed by the these pairs, i.e., 92, 28, 81, 16, 60, 09, are present in the last 2 digits of some rotation. Thus, checking divisibility of these pairs gives the required number of rotations. Note: A single digit number can directly be checked for divisibility.
Below is the implementation of the approach.
Time Complexity : O(n) where n is number of digits in input number.
This article is contributed by Ayush Jauhari. 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.
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.
- Count rotations divisible by 8
- Count rotations which are divisible by 10
- Count rotations of N which are Odd and Even
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Count of rotations required to generate a sorted array
- Count rotations in sorted and rotated linked list
- 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]
- Count of sub-strings that are divisible by K
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count sub-matrices having sum divisible 'k'
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count all sub-arrays having sum divisible by k
- Count sub-arrays whose product is divisible by k
- Count pairs in array whose sum is divisible by 4
- Count of longest possible subarrays with sum not divisible by K
- Count pairs in array whose sum is divisible by K
- Count the numbers divisible by 'M' in a given range
- Count integers in the range [A, B] that are not divisible by C and D
- Count divisible pairs in an array