N-th Tribonacci Number - Problem
The Tribonacci sequence Tn is defined as follows:
- T0 = 0
- T1 = 1
- T2 = 1
- Tn+3 = Tn + Tn+1 + Tn+2 for n ≥ 0
Given n, return the value of Tn.
Note: This is a variation of the famous Fibonacci sequence where each term is the sum of the three preceding ones instead of two.
Input & Output
Example 1 — Small Value
$
Input:
n = 4
›
Output:
4
💡 Note:
T(4) = T(1) + T(2) + T(3) = 1 + 1 + 2 = 4. The sequence is: T(0)=0, T(1)=1, T(2)=1, T(3)=2, T(4)=4.
Example 2 — Base Case
$
Input:
n = 0
›
Output:
0
💡 Note:
T(0) = 0 by definition. This is one of the base cases.
Example 3 — Another Base Case
$
Input:
n = 2
›
Output:
1
💡 Note:
T(2) = 1 by definition. Both T(1) and T(2) equal 1 as base cases.
Constraints
- 0 ≤ n ≤ 37
- The answer is guaranteed to fit in a 32-bit integer
Visualization
Tap to expand
Understanding the Visualization
1
Base Cases
T(0)=0, T(1)=1, T(2)=1 are given
2
Pattern
Each T(n) = T(n-1) + T(n-2) + T(n-3)
3
Result
Build sequence up to T(n)
Key Takeaway
🎯 Key Insight: Each Tribonacci number is the sum of the three preceding numbers, making it perfect for dynamic programming optimization
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code