Given a range, count Fibonacci numbers in given range. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ..
Input: low = 10, high = 100 Output: 5 There are five Fibonacci numbers in given range, the numbers are 13, 21, 34, 55 and 89. Input: low = 10, high = 20 Output: 1 There is only one Fibonacci Number, 13. Input: low = 0, high = 1 Output: 3 Fibonacci numbers are 0, 1 and 1
We strongly recommend you to minimize your browser and try this yourself first.
A Brute Force Solution is to one by one find all Fibonacci Numbers and count all Fibonacci numbers in given range
An Efficient Solution is to use previous Fibonacci Number to generate next using simple Fibonacci formula that fn = fn-1 + fn-2.
Count of Fibonacci Numbers is 5
Time Complexity Analysis:
Consider the that Fibonacci Numbers can be written as below
So the value of Fibonacci numbers grow exponentially. It means that the while loop grows exponentially till it reaches ‘high’. So the loop runs O(Log (high)) times.
One solution could be directly use above formula to find count of Fibonacci Numbers, but that is not practically feasible (See this for details).
Auxiliary Space: O(1)
This article is contributed by Sudhanshu Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Array range queries to count the number of Fibonacci numbers with updates
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Last digit of sum of numbers in the given range in the Fibonacci series
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Space efficient iterative method to Fibonacci number
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Sum of Fibonacci Numbers in a range
- Sum of all Non-Fibonacci numbers in a range for Q queries
- Count of Fibonacci pairs with sum N in range 0 to N
- Count composite fibonacci numbers from given array
- Find Index of given fibonacci number in constant time
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Find maximum in a stack in O(1) time and O(1) extra space
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Time complexity of recursive Fibonacci program
- Time-Space Trade-Off in Algorithms