Find the Minimum Number of Fibonacci Numbers Whose Sum Is K - Problem

Given an integer k, find the minimum number of Fibonacci numbers whose sum equals k. The same Fibonacci number can be used multiple times.

The Fibonacci sequence is defined as:

  • F₁ = 1
  • F₂ = 1
  • Fₙ = Fₙ₋₁ + Fₙ₋₂ for n > 2

So the sequence starts: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

Goal: Return the minimum count of Fibonacci numbers that sum to k.

Note: It's guaranteed that a solution always exists for the given constraints.

Input & Output

example_1.py — Basic case
$ Input: k = 7
Output: 2
💡 Note: The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, ... We can represent 7 as 5 + 2, which uses 2 Fibonacci numbers. This is optimal since we can't represent 7 with just 1 Fibonacci number.
example_2.py — Perfect Fibonacci
$ Input: k = 10
Output: 2
💡 Note: We can represent 10 as 8 + 2. Both 8 and 2 are Fibonacci numbers. We use the greedy approach: pick the largest possible (8), then the largest possible from what remains (2).
example_3.py — Small value
$ Input: k = 4
Output: 2
💡 Note: We can represent 4 as 3 + 1. Although we could also use 2 + 1 + 1, the greedy approach gives us 3 + 1 which uses only 2 numbers and is optimal.

Constraints

  • 1 ≤ k ≤ 109
  • Note: k can be very large, but there are only ~45 Fibonacci numbers ≤ 109

Visualization

Tap to expand
Making Change with Fibonacci Coins (k = 15)11235813Available Fibonacci CoinsStep 1k = 15Pick: 13 (largest ≤ 15)Step 2k = 2Pick: 2 (largest ≤ 2)Result15 = 13 + 2Count: 2
Understanding the Visualization
1
Generate Fibonacci Sequence
Create all Fibonacci numbers up to k: [1, 1, 2, 3, 5, 8, 13, 21, 34, ...]
2
Pick Largest Possible
Always choose the biggest Fibonacci number that doesn't exceed remaining k
3
Subtract and Repeat
Reduce k by chosen number and increment counter, repeat until k = 0
4
Return Count
The counter holds the minimum number of Fibonacci numbers needed
Key Takeaway
🎯 Key Insight: Fibonacci numbers have the unique property that F(n+1) > F(n) + F(n-1) + ... + F(1), making the greedy approach optimal!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
38.2K Views
Medium Frequency
~12 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen