Prerequisites: Digit DP
Given a range [L, R], the task is to count the numbers in this range having the difference between, the sum of digits at even positions and sum of digits at odd positions, as a Fibonacci Number.
Note: Consider the position of the least significant digit in the number as an odd position.
Input: L = 1, R = 10
The only number which satisfies the given condition is 10.
Input: L = 50, R = 100
- A four-dimensional table is formed and at every recursive call, we need to check whether the required difference is a Fibonacci number or not.
- Since the highest number in the range is 1018, the maximum sum at either even or odd positions can be at max 9 times 9 and hence the maximum difference. So, we need to check only Fibonacci numbers only up to 100 at the base condition.
- To check whether the number is Fibonacci or not, generate all the Fibonacci numbers and create a hash set.
The following are the DP states of the table:
- Since we can consider our number as a sequence of digits, one state is the position at which we are currently at. This position can have values from 0 to 18 if we are dealing with the numbers up to 1018. In each recursive call, we try to build the sequence from left to right by placing a digit from 0 to 9.
- First state is the sum of the digits at even positions we have placed so far.
- Second state is the sum of the digits at odd positions we have placed so far.
- Another state is the boolean variable tight which tells the number we are trying to build has already become smaller than R so that in the upcoming recursive calls we can place any digit from 0 to 9. If the number has not become smaller, the maximum limit of digit we can place at the current position in R.
Below is the implementation of the above approach:
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 Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Sum of Fibonacci Numbers in a range
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Last digit of sum of numbers in the given range in the Fibonacci series
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Array range queries to count the number of Fibonacci numbers with updates
- Count of numbers with all digits same in a given range
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Cumulative product of digits of all numbers in the given range
- Total numbers with no repeated digits in a range
- Count numbers in range such that digits in it and it's product with q are unequal
- Pair of fibonacci numbers with a given sum and minimum absolute difference
- Queries for maximum and minimum difference between Fibonacci numbers in given ranges
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
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.