advanced
Big O Optimization Challenge
Optimize a specific recursive algorithm to improve time and space complexity.
📝 Prompt Inhoud
Consider a recursive function that calculates the nth Fibonacci number but has exponential time complexity. Rewrite this function to achieve O(n) time complexity and O(1) space complexity using an iterative approach or matrix exponentiation. Explain the mathematical reasoning behind the optimization and the trade-offs involved.