**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**.

**Examples:**

Input:N = 10, K = 3

Output:2

Explanation:

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

Output:11

**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.

**Proof:**

- Let the function
Cdenotes the count of fibonacci numbers upto number_{i}i.- Now, for the number
X + 1the difference is X + 1 – C_{X + 1}which is greater than or equal to the difference X – C_{X}for the number X, i.e.(X + 1 – C._{X + 1}) ≥ (X – C_{X})- Thus, if
(X – C, then_{X}) ≥ S(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:

## C/C++

`// C++ program to find the count ` `// of numbers whose difference with ` `// Fibonacci count upto them is atleast K ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MAX = 1000005; ` ` ` `// fibUpto[i] denotes the count of ` `// fibonacci numbers upto i ` `int` `fibUpto[MAX + 1]; ` ` ` `// Function to compute all the Fibonacci ` `// numbers and update fibUpto array ` `void` `compute(` `int` `sz) ` `{ ` ` ` `bool` `isFib[sz + 1]; ` ` ` `memset` `(isFib, ` `false` `, ` `sizeof` `(isFib)); ` ` ` ` ` `// Store the first two Fibonacci numbers ` ` ` `int` `prev = 0, curr = 1; ` ` ` `isFib[prev] = isFib[curr] = ` `true` `; ` ` ` ` ` `// Compute the Fibonacci numbers ` ` ` `// and store them in isFib array ` ` ` `while` `(curr <= sz) { ` ` ` `int` `temp = curr + prev; ` ` ` `isFib[temp] = ` `true` `; ` ` ` `prev = curr; ` ` ` `curr = temp; ` ` ` `} ` ` ` ` ` `// Compute fibUpto array ` ` ` `fibUpto[0] = 1; ` ` ` `for` `(` `int` `i = 1; i <= sz; i++) { ` ` ` `fibUpto[i] = fibUpto[i - 1]; ` ` ` `if` `(isFib[i]) ` ` ` `fibUpto[i]++; ` ` ` `} ` `} ` ` ` `// Function to return the count ` `// of valid numbers ` `int` `countOfNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Compute fibUpto array ` ` ` `compute(N); ` ` ` ` ` `// Binary search to find the minimum ` ` ` `// number that follows the condition ` ` ` `int` `low = 1, high = N, ans = 0; ` ` ` `while` `(low <= high) { ` ` ` `int` `mid = (low + high) >> 1; ` ` ` ` ` `// Check if the number is ` ` ` `// valid, try to reduce it ` ` ` `if` `(mid - fibUpto[mid] >= K) { ` ` ` `ans = mid; ` ` ` `high = mid - 1; ` ` ` `} ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` ` ` ` ` `// Ans is the minimum valid number ` ` ` `return` `(ans ? N - ans + 1 : 0); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 10, K = 3; ` ` ` ` ` `cout << countOfNumbers(N, K); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the count ` `// of numbers whose difference with ` `// Fibonacci count upto them is atleast K ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `int` `MAX = ` `1000005` `; ` ` ` `// fibUpto[i] denotes the count of ` `// fibonacci numbers upto i ` `static` `int` `[]fibUpto = ` `new` `int` `[MAX + ` `1` `]; ` ` ` `// Function to compute all the Fibonacci ` `// numbers and update fibUpto array ` `static` `void` `compute(` `int` `sz) ` `{ ` ` ` `boolean` `[]isFib = ` `new` `boolean` `[sz + ` `1` `]; ` ` ` ` ` `// Store the first two Fibonacci numbers ` ` ` `int` `prev = ` `0` `, curr = ` `1` `; ` ` ` `isFib[prev] = isFib[curr] = ` `true` `; ` ` ` ` ` `// Compute the Fibonacci numbers ` ` ` `// and store them in isFib array ` ` ` `while` `(curr <= sz) { ` ` ` `int` `temp = curr + prev; ` ` ` `if` `(temp <= sz) ` ` ` `isFib[temp] = ` `true` `; ` ` ` `prev = curr; ` ` ` `curr = temp; ` ` ` `} ` ` ` ` ` `// Compute fibUpto array ` ` ` `fibUpto[` `0` `] = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= sz; i++) { ` ` ` `fibUpto[i] = fibUpto[i - ` `1` `]; ` ` ` `if` `(isFib[i]) ` ` ` `fibUpto[i]++; ` ` ` `} ` `} ` ` ` `// Function to return the count ` `// of valid numbers ` `static` `int` `countOfNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Compute fibUpto array ` ` ` `compute(N); ` ` ` ` ` `// Binary search to find the minimum ` ` ` `// number that follows the condition ` ` ` `int` `low = ` `1` `, high = N, ans = ` `0` `; ` ` ` `while` `(low <= high) { ` ` ` `int` `mid = (low + high) >> ` `1` `; ` ` ` ` ` `// Check if the number is ` ` ` `// valid, try to reduce it ` ` ` `if` `(mid - fibUpto[mid] >= K) { ` ` ` `ans = mid; ` ` ` `high = mid - ` `1` `; ` ` ` `} ` ` ` `else` ` ` `low = mid + ` `1` `; ` ` ` `} ` ` ` ` ` `// Ans is the minimum valid number ` ` ` `return` `(ans>` `0` `? N - ans + ` `1` `: ` `0` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `10` `, K = ` `3` `; ` ` ` ` ` `System.out.print(countOfNumbers(N, K)); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the count ` `# of numbers whose difference with ` `# Fibonacci count upto them is atleast K ` ` ` `MAX` `=` `1000005` ` ` `# fibUpto[i] denotes the count of ` `# fibonacci numbers upto i ` `fibUpto ` `=` `[` `0` `]` `*` `(` `MAX` `+` `1` `) ` ` ` `# Function to compute all the Fibonacci ` `# numbers and update fibUpto array ` `def` `compute(sz): ` ` ` ` ` `isFib ` `=` `[` `False` `]` `*` `(sz ` `+` `1` `) ` ` ` `# Store the first two Fibonacci numbers ` ` ` `prev ` `=` `0` ` ` `curr ` `=` `1` ` ` `isFib[prev] ` `=` `True` ` ` `isFib[curr] ` `=` `True` ` ` ` ` `# Compute the Fibonacci numbers ` ` ` `# and store them in isFib array ` ` ` `while` `(curr <` `=` `sz): ` ` ` `temp ` `=` `curr ` `+` `prev ` ` ` `if` `(temp<` `=` `sz): ` ` ` `isFib[temp] ` `=` `True` ` ` `prev ` `=` `curr ` ` ` `curr ` `=` `temp ` ` ` ` ` `# Compute fibUpto array ` ` ` `fibUpto[` `0` `] ` `=` `1` ` ` `for` `i ` `in` `range` `( ` `1` `,sz` `+` `1` `): ` ` ` `fibUpto[i] ` `=` `fibUpto[i ` `-` `1` `] ` ` ` `if` `(isFib[i]): ` ` ` `fibUpto[i]` `+` `=` `1` ` ` `# Function to return the count ` `# of valid numbers ` `def` `countOfNumbers(N, K): ` ` ` ` ` `# Compute fibUpto array ` ` ` `compute(N) ` ` ` ` ` `# Binary search to find the minimum ` ` ` `# number that follows the condition ` ` ` `low , high, ans ` `=` `1` `, N, ` `0` ` ` `while` `(low <` `=` `high): ` ` ` `mid ` `=` `(low ` `+` `high) >> ` `1` ` ` ` ` `# Check if the number is ` ` ` `# valid, try to reduce it ` ` ` `if` `(mid ` `-` `fibUpto[mid] >` `=` `K): ` ` ` `ans ` `=` `mid ` ` ` `high ` `=` `mid ` `-` `1` ` ` `else` `: ` ` ` `low ` `=` `mid ` `+` `1` ` ` ` ` `# Ans is the minimum valid number ` ` ` `if` `(ans): ` ` ` `return` `(N ` `-` `ans ` `+` `1` `) ` ` ` `return` `0` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `10` ` ` `K ` `=` `3` ` ` ` ` `print` `(countOfNumbers(N, K)) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the count ` `// of numbers whose difference with ` `// Fibonacci count upto them is atleast K ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `static` `int` `MAX = 1000005; ` ` ` `// fibUpto[i] denotes the count of ` `// fibonacci numbers upto i ` `static` `int` `[]fibUpto = ` `new` `int` `[MAX + 1]; ` ` ` `// Function to compute all the Fibonacci ` `// numbers and update fibUpto array ` `static` `void` `compute(` `int` `sz) ` `{ ` ` ` `bool` `[]isFib = ` `new` `bool` `[sz + 1]; ` ` ` ` ` `// Store the first two Fibonacci numbers ` ` ` `int` `prev = 0, curr = 1; ` ` ` `isFib[prev] = isFib[curr] = ` `true` `; ` ` ` ` ` `// Compute the Fibonacci numbers ` ` ` `// and store them in isFib array ` ` ` `while` `(curr <= sz) { ` ` ` `int` `temp = curr + prev; ` ` ` `if` `(temp <= sz) ` ` ` `isFib[temp] = ` `true` `; ` ` ` `prev = curr; ` ` ` `curr = temp; ` ` ` `} ` ` ` ` ` `// Compute fibUpto array ` ` ` `fibUpto[0] = 1; ` ` ` `for` `(` `int` `i = 1; i <= sz; i++) { ` ` ` `fibUpto[i] = fibUpto[i - 1]; ` ` ` `if` `(isFib[i]) ` ` ` `fibUpto[i]++; ` ` ` `} ` `} ` ` ` `// Function to return the count ` `// of valid numbers ` `static` `int` `countOfNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Compute fibUpto array ` ` ` `compute(N); ` ` ` ` ` `// Binary search to find the minimum ` ` ` `// number that follows the condition ` ` ` `int` `low = 1, high = N, ans = 0; ` ` ` `while` `(low <= high) { ` ` ` `int` `mid = (low + high) >> 1; ` ` ` ` ` `// Check if the number is ` ` ` `// valid, try to reduce it ` ` ` `if` `(mid - fibUpto[mid] >= K) { ` ` ` `ans = mid; ` ` ` `high = mid - 1; ` ` ` `} ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` ` ` ` ` `// Ans is the minimum valid number ` ` ` `return` `(ans>0 ? N - ans + 1 : 0); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 10, K = 3; ` ` ` ` ` `Console.WriteLine(countOfNumbers(N, K)); ` `} ` `} ` ` ` `// This code is contributed by Mohitkumar29 ` |

*chevron_right*

*filter_none*

**Output:**

2

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.

## Recommended Posts:

- Count numbers < = N whose difference with the count of primes upto them is > = K
- Sum of Fibonacci numbers at even indexes upto N terms
- Python | Find fibonacci series upto n using lambda
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of numbers upto M divisible by given Prime Numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Split N into two integers whose addition to A and B makes them equal
- Largest subset whose all elements are Fibonacci numbers
- Find two Fibonacci numbers whose sum can be represented as N
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Sort numbers based on count of letters required to represent them in words
- 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
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count the pairs in an array such that the difference between them and their indices is equal
- Count the nodes whose sum with X is a Fibonacci number
- Count nodes in the given tree whose weight is a fibonacci number
- Maximise the sum of two Numbers using at most one swap between them

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.