Given a large positive number as string, count all rotations of the given number which are divisible by 8.
Input: 8 Output: 1 Input: 40 Output: 1 Rotation: 40 is divisible by 8 04 is not divisible by 8 Input : 13502 Output : 0 No rotation is divisible by 8 Input : 43262488612 Output : 4
Approach: For large numbers it is difficult to rotate and divide each number by 8. Therefore, ‘divisibility by 8’ property is used which says that a number is divisible by 8 if the last 3 digits of the number is divisible by 8. Here we do not actually rotate the number and check last 8 digits for divisibility, instead we count consecutive sequence of 3 digits (in circular way) which are divisible by 8.
Consider a number 928160 Its rotations are 928160, 092816, 609281, 160928, 816092, 281609. Now form consecutive sequence of 3-digits from the original number 928160 as mentioned in the approach. 3-digit: (9, 2, 8), (2, 8, 1), (8, 1, 6), (1, 6, 0),(6, 0, 9), (0, 9, 2) We can observe that the 3-digit number formed by the these sets, i.e., 928, 281, 816, 160, 609, 092, are present in the last 3 digits of some rotation. Thus, checking divisibility of these 3-digit numbers gives the required number of rotations.
Time Complexity : O(n), where n is the number of digits in input number.
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.
- Count rotations divisible by 4
- Count rotations which are divisible by 10
- Count rotations in sorted and rotated linked list
- Count rotations of N which are Odd and Even
- Count of rotations required to generate a sorted array
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- 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]
- A Program to check if strings are rotations of each other or not
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Maximum sum of i*arr[i] among all rotations of a given array
- Check if all rows of a matrix are circular rotations of each other
- Check if two numbers are bit rotations of each other or not
- Quickly find multiple left rotations of an array | Set 1
- Minimum rotations required to get the same string
- Find element at given index after a number of rotations
- Minimum rotations to unlock a circular lock
- Check if strings are rotations of each other or not | Set 2
- Generating numbers that are divisor of their right-rotations
- Maximum contiguous 1 possible in a binary string after k rotations
- Check whether all the rotations of a given number is greater than or equal to the given number or not
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.