Knowledge Guide
HomeDSARecursion

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#InputOutputExplanation
150, 1, 1, 2, 3The Fibonacci series up to the 5th term is 0, 1, 1, 2, 3.
280, 1, 1, 2, 3, 5, 8, 13The Fibonacci series up to the 8th term is 0, 1, 1, 2, 3, 5, 8, 13.
3120, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55The 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:

  1. Initialize a memoization table to store the Fibonacci numbers.

  2. Implement the recursive function fibonacci that takes n

    as input.

    • Check if n is 0 or 1. If so, return n.
    • Check if the Fibonacci number at position n is already present in the memoization table. If so, return the value.
    • Otherwise, calculate the Fibonacci number by recursively calling the fibonacci function for n-1 and n-2, summing the results, and store it in the memoization table.
  3. In the main function, take input n from the user.

  4. Call the fibonacci function with n as input and print the Fibonacci series up to the nth term.

Image
Image

Code

Here is the code for this algorithm:

java
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 , where n is the input number. This is because we calculate the Fibonacci numbers from 0 to n, and each Fibonacci number is calculated only once and stored in the memoization table for future use.

The space complexity is also because we store the Fibonacci numbers in the memoization table, which can hold up to n entries. Additionally, the recursion stack occupies space proportional to the input number n.

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

🪜 Hint ladder (no spoilers)

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.
🎨 Explain the approach visually

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.
🔍 Review my solution

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.
🔁 Drill the pattern

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.

📝 My notes