Given a range [L, R], the task is to count the numbers which have even number of odd digits and odd number of even digits. For example,
- 8 has 1 even digit and 0 odd digit – Satisfies the condition since 1 is odd and 0 is even.
- 545 has 1 even digit and 2 odd digits – Satisfies the condition since 1 is odd and 2 is even.
- 4834 has 3 even digits and 1 odd digit – Does not satisfy the condition since there are odd numbers(i.e 1) of odd digits.
Input: L = 1, R = 9
2, 4, 6 and 8 are the only integers from the
given range that satisfy the given conditions.
Input: L = 1, R = 19
Input: L = 123, R = 984
- Case 1
There is a pattern in which these numbers occur between
and (where 1<=k<=18).
Number of occurrences from
- 1 – 10 and 1 – 100 are 4
- 1 – 1000 and 1 – 10000 are 454
- 1 – 10000 and 1 – 100000 are 45454
and so on…
- If the number of digits in a number is even then it cannot satisfy the given condition because we need an odd number(of digits) and an even number(of digits) to satisfy our condition and odd number + even number is always odd
- So if the number of digits for a given number(say n) is even then its number of occurrences from 1 is equal to the number of occurrences from
to largest (1<=k<=18) which is less than n
Let n = 19, number of digits in 19 are 2
Therefore number of occurrences from 1 – 19 = number of occurrences from 1 – 10 (since 10 the largest
less than 19)
If number of digits for a given number(say n) are odd then number of occurrences between
and n is equal to
is the largest less than n.
Implementation: Now we now how to calculate the number of occurrences from 1 to given n. Therefore,
Number of occurrences from L to R = NumberOfOccurrencesUpto(R) – NumberOfOccurrencesUpto(L – 1) where L is not equal to 1.
Below is the implementation of the above approach:
Time Complexity: O(logn)
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Count of numbers in range which are divisible by M and have digit D at odd places
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count elements in the given range which have maximum number of divisors
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count integers in a range which are divisible by their euler totient value
- Count ways to express even number ‘n’ as sum of even integers
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count number of rotated strings which have more number of vowels in the first half than second half
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Count the numbers < N which have equal number of divisors as K
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Find the count of M character words which have at least one character repeated
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count rotations of N which are Odd and Even
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.