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 numbers in given range such that sum of even digits is greater than sum of odd digits
- Even numbers at even index and odd numbers at odd index
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count subarrays having sum of elements at even and odd positions equal
- 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
- Positive elements at even and negative at odd positions (Relative order not maintained)
- Generate an array of given size with equal count and sum of odd and even numbers
- Last digit of sum of numbers in the given range in the Fibonacci series
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Reverse the elements only at odd positions in the given Array
- Find the sum of first N odd Fibonacci numbers
- Even Fibonacci Numbers Sum
- Sum of Fibonacci numbers at even indexes upto N terms
- Queries for maximum and minimum difference between Fibonacci numbers in given ranges
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Pair of fibonacci numbers with a given sum and minimum absolute difference
- Check if sum of Fibonacci elements in an Array is a Fibonacci 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.