You are given an integer n. Your task is to return the nth Fibonacci number using an efficient approach.
The Fibonacci sequence starts with F(1) = 0 and F(2) = 1. Each subsequent term is calculated as the sum of the previous two terms.
Input: n = 10
Output: 34
Explanation: The 10th term of the Fibonacci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 is 34.
Input: n = 1
Output: 0
Explanation: The first Fibonacci number is 0.
Input: n = 5
Output: 3
Explanation: The 5th term of the Fibonacci sequence is 3.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1
0
2
1