Prerequisites: Binary Search
Given two positive integers N and K, the task is to count all the numbers that satisfy the following conditions:
If the number is num,
- num ≤ N.
- abs(num – count) ≥ K where count is the count of fibonacci numbers upto num.
Input: N = 10, K = 3
9 and 10 are the valid numbers which satisfy the given conditions.
For 9, the difference between 9 and fibonacci numbers upto 9 (0, 1, 2, 3, 5, 8) is i.e. 9 – 6 = 3.
For 10, the difference between 9 and fibonacci numbers upto 10 (0, 1, 2, 3, 5, 8) is i.e. 10 – 6 = 4.
Input: N = 30, K = 7
Observation: On observing carefully, the function which is the difference of the number and count of fibonacci numbers upto that number is a monotonically increasing function for a particular K. Also, if a number X is a valid number then X + 1 will also be a valid number.
- Let the function Ci denotes the count of fibonacci numbers upto number i.
- Now, for the number X + 1 the difference is X + 1 – CX + 1 which is greater than or equal to the difference X – CX for the number X, i.e. (X + 1 – CX + 1) ≥ (X – CX).
- Thus, if (X – CX) ≥ S, then (X + 1 – CX + 1) ≥ S.
Approach: Therefore, from the above observation, the idea is to use hashing to precompute and store the Fibonacci nodes up to the maximum value and create a prefix array using the prefix sum array concept where every index stores the number of Fibonacci numbers less than ‘i’ to make checking easy and efficient (in O(1) time).
Now, we can use binary search to find the minimum valid number X, as all the numbers in range [X, N] are valid. Therefore, the answer would be N – X + 1.
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 < = N whose difference with the count of primes upto them is > = K
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count of numbers upto M divisible by given Prime Numbers
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- 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
- Sum of Fibonacci numbers at even indexes upto N terms
- Array range queries to count the number of Fibonacci numbers with updates
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count subarrays with sum as difference of squares of two numbers
- Count of subarrays whose product is equal to difference of two different numbers
- Queries for the difference between the count of composite and prime numbers in a given range
- Count numbers with difference between number and its digit sum greater than specific value
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Count the nodes whose sum with X is a Fibonacci number
- Count of Fibonacci pairs with sum N in range 0 to N
- Count of Fibonacci divisors of a given number
- Count of consecutive Fibonacci pairs in the 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.