Solution Fibonacci Series Using Memoization
Problem Statement
Print Fibonacci Series Using Memoization and Recursion.
Given a positive integer n, print the Fibonacci series up to the nth term using memoization and recursion.
Examples:
| Sr# | Input | Output | Explanation |
|---|---|---|---|
| 1 | 5 | 0, 1, 1, 2, 3 | The Fibonacci series up to the 5th term is 0, 1, 1, 2, 3. |
| 2 | 8 | 0, 1, 1, 2, 3, 5, 8, 13 | The Fibonacci series up to the 8th term is 0, 1, 1, 2, 3, 5, 8, 13. |
| 3 | 12 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 | The Fibonacci series up to the 12th term is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. |
Solution:
The Fibonacci series can be calculated using memoization and recursion. The key idea is to store the previously calculated Fibonacci numbers in a memoization table to avoid redundant calculations. We define a recursive function fibonacci that takes an input n and returns the Fibonacci number at position n.
The base case occurs when n is 0 or 1, in which case we return n as the Fibonacci number. For any other value of n, we check if the Fibonacci number at position n is already calculated and stored in the memoization table. If it is, we directly return the value. Otherwise, we recursively calculate the Fibonacci number by summing the previous two Fibonacci numbers (calculated recursively) and store it in the memoization table for future use.
The overall algorithm follows these steps:
-
Initialize a memoization table to store the Fibonacci numbers.
-
Implement the recursive function
fibonaccithat takesnas input.
- Check if
nis 0 or 1. If so, returnn. - Check if the Fibonacci number at position
nis already present in the memoization table. If so, return the value. - Otherwise, calculate the Fibonacci number by recursively calling the
fibonaccifunction forn-1andn-2, summing the results, and store it in the memoization table.
- Check if
-
In the main function, take input
nfrom the user. -
Call the
fibonaccifunction withnas input and print the Fibonacci series up to thenth term.
Code
Here is the code for this algorithm:
import java.util.HashMap;
import java.util.Map;
public class Solution {
private static Map<Integer, Integer> memoizationTable;
public static int fibonacci(int n) {
if (n == 0 || n == 1) {
return n; // Base case: return n if n is 0 or 1
}
// Check if Fibonacci number is already calculated and stored in memoization table
if (memoizationTable.containsKey(n)) {
return memoizationTable.get(n);
}
// Calculate Fibonacci number using recursion
int fib = fibonacci(n - 1) + fibonacci(n - 2);
// Store Fibonacci number in memoization table for future use
memoizationTable.put(n, fib);
return fib;
}
public static void main(String[] args) {
memoizationTable = new HashMap<>();
// Example inputs
int[] examples = { 5, 8, 12 };
for (int n : examples) {
System.out.print("Fibonacci Series up to " + n + ": ");
for (int i = 0; i <= n; i++) {
System.out.print(fibonacci(i) + " ");
}
System.out.println();
}
}
}
Time and Space Complexity
The time complexity of the Fibonacci series algorithm using memoization and recursion is
The space complexity is also
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Fibonacci Series Using Memoization? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Solution Fibonacci Series Using Memoization** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Solution Fibonacci Series Using Memoization** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Solution Fibonacci Series Using Memoization**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Solution Fibonacci Series Using Memoization**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.