Given a number N, the task is to count all the rotations of the given number which are divisible by 10.
Input: N = 10203
There are 5 rotations possible for the given number. They are: 02031, 20310, 03102, 31020, 10203
Out of these rotations, only 20310 and 31020 are divisible by 10. So 2 is the output.
Input: N = 135
Naive Approach: The naive approach for this problem is to form all the possible rotations. It is known that for a number of size K, the number of possible rotations for this number N is K. Therefore, find all the rotations and for every rotation, check if the number is divisible by 10 or not. The time complexity for this approach is quadratic.
Efficient Approach: The efficient approach lies behind the concept that in order to check whether a number is divisible by 10 or not, we simply check if the last digit is 0. So, the idea is to simply iterate over the given number and find the count of 0’s. If the count of 0’s is F, then clearly, F out of K rotations will have 0 at the end of the given number N.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the 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 divisible by 8
- Count rotations of N which are Odd and Even
- Number of strings which starts and ends with same character after rotations
- Find permutation of n which is divisible by 3 but not divisible by 6
- Count rotations in sorted and rotated linked list
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Count of rotations required to generate a sorted array
- 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 numbers in range 1 to N which are divisible by X but not by Y
- Count numbers which are divisible by all the numbers from 2 to 10
- Count integers in a range which are divisible by their euler totient value
- Count all prefixes of the given binary array which are divisible by x
- Count of elements on the left which are divisible by current element
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count of pairs of Array elements which are divisible by K when concatenated
- Count of elements on the left which are divisible by current element | Set 2
- 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
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.