Skip to main content

N-th Tribonacci Number

Easy
Data Structures and Algorithms
Editorial
Topics:
Dynamic Programming
Iteration
Math

The Tribonacci sequence is defined by three initial values: T0 = 0, T1 = 1, and T2 = 1. For every index k >= 3, the next value is the sum of the three preceding values: Tk = T(k - 1) + T(k - 2) + T(k - 3).

Given the integer parameter n, return Tn, the Tribonacci value at index n. The method must return the result as an integer.

Example 1:

Input: n = 2
Output: 1
Explanation: The second initial Tribonacci value is T2 = 1.

Example 2:

Input: n = 6
Output: 13
Explanation: Starting with 0, 1, 1, each later value sums the previous three, giving T6 = 13.

Example 3:

Input: n = 0
Output: 0
Explanation: The sequence is defined with T0 = 0.

Constraints:

  • n is an integer.
  • 0 <= n <= 37.
  • The answer fits in a 32-bit signed integer.

Need help or want more practice?

Practise in an interviewGet AI Coach help