N-th Tribonacci Number

The Tribonacci sequence is defined by T(0) = 0, T(1) = 1, T(2) = 1, and T(n + 3) = T(n) + T(n + 1) + T(n + 2) for n >= 0. Given n, return the value of T(n).

30:00

N-th Tribonacci Number
easy
Free written solution
Topics
Companies

The Tribonacci sequence is defined by T(0) = 0, T(1) = 1, T(2) = 1, and T(n + 3) = T(n) + T(n + 1) + T(n + 2) for n >= 0. Given n, return the value of T(n).

Example 1:
Input: {"n":4}
Output: 4
Constraints:
  • 0≤n≤370 \leq n \leq 37

  • The answer fits in a 32-bit integer, so T(n)≤231−1T(n) \leq 2^{31} - 1.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":4}

Each Tribonacci term is the sum of the three terms before it. We want T(4).

Three rolling slots

a

-

+

b

-

+

c

-

=

next

-

T(0) .. T(4)

T(i) = T(i-3) + T(i-2) + T(i-1)

?

T(0)

?

T(1)

?

T(2)

?

T(3)

?

T(4)

Variables
VariableValue
n4
DepthFunction Call
Stack empty
0/10