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

Given an integer k, return the minimum number of Fibonacci numbers whose sum is equal to k. The same Fibonacci number can be used multiple times.

The Fibonacci numbers are defined as:

  • F1 = 1
  • F2 = 1
  • Fn = Fn-1 + Fn-2 for n > 2

It is guaranteed that for the given constraints we can always find such Fibonacci numbers that sum up to k.

Input & Output

Example 1 — Basic Case
$ Input: k = 7
Output: 2
💡 Note: The Fibonacci numbers are 1, 1, 2, 3, 5, 8... We can represent 7 as 5 + 2, so we need minimum 2 Fibonacci numbers
Example 2 — Single Number
$ Input: k = 8
Output: 1
💡 Note: 8 is already a Fibonacci number, so we only need 1 Fibonacci number
Example 3 — Larger Number
$ Input: k = 15
Output: 2
💡 Note: 15 can be represented as 13 + 2, requiring 2 Fibonacci numbers

Constraints

  • 1 ≤ k ≤ 109

Visualization

Tap to expand
Minimum Fibonacci Numbers to Sum K INPUT Target Sum: k = 7 k = 7 Fibonacci Sequence: 1 1 2 3 5 8 F1 F2 F3 F4 F5 F6 Valid Fibonacci numbers less than or equal to 7: [1, 1, 2, 3, 5] Greedy: Pick largest first ALGORITHM STEPS 1 Generate Fibonacci Build sequence up to k 2 Pick Largest Fib <= k 5 <= 7, pick 5 Greedy Iterations: Iter 1: k=7, pick 5 k = 7-5 = 2 Iter 2: k=2, pick 2 k = 2-2 = 0 3 Subtract and Count k=0, count=2, Done! 4 Return Count Minimum = 2 numbers FINAL RESULT Sum Composition: 5 + 2 = 7 F5 F3 Output: 2 OK - Verified! 5 + 2 = 7 Both are Fibonacci Minimum count achieved Key Insight: The Greedy approach works because Fibonacci numbers have a special property: any number can be represented as a sum of non-consecutive Fibonacci numbers (Zeckendorf's theorem). Always picking the largest Fibonacci number <= remaining sum guarantees the minimum count. TutorialsPoint - Find the Minimum Number of Fibonacci Numbers Whose Sum Is K | Greedy Approach
Asked in
Google 15 Facebook 12
23.4K Views
Medium Frequency
~15 min Avg. Time
890 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