# N-th Tribonacci Number – Leetcode Solution

In this post, we are going to solve the problem, 1137. N-th Tribonacci Number problem of Leetcode. This problem 1137. N-th Tribonacci Number is a Leetcode easy level problem. Let’s see the code, 1137. N-th Tribonacci Number – Leetcode Solution.

## Problem

The Tribonacci sequence Tn is defined as follows:

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given n, return the value of Tn.

### Example 1 :

``````Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4``````

### Example 2 :

``````Input: n = 25
Output: 1389537``````

### Constraints

• `0 <= n <= 37`
• The answer is guaranteed to fit within a 32-bit integer, ie. answer <= `2^31 - 1.`

Now, let’s see the code of 1137. N-th Tribonacci Number – Leetcode Solution.

# N-th Tribonacci Number – Leetcode Solution

### 1137. N-th Tribonacci Number – Solution in Java

```class Solution {
public int tribonacci(int n) {
int a = 0, b = 1, c = 1, ans = 0;
if(n==0) return 0;
else if(n==1) return 1;
else if(n==2) return 1;
for(int N=3;N<=n;N++){
ans = a + b + c;
a = b;
b = c;
c = ans;
}
return ans;
}
}```

### 1137. N-th Tribonacci Number – Solution in C++

```    int tribonacci(int n) {
if (n < 2) return n;
int a = 0, b = 1, c = 1, d = a + b + c;
while (n-- > 2) {
d = a + b + c, a = b, b = c, c = d;
}
return c;
}```

### 1137. N-th Tribonacci Number – Solution in Python

```    def tribonacci(self, n):
a, b, c = 1, 0, 0
for _ in xrange(n): a, b, c = b, c, a + b + c
return c```

Note: This problem 1137. N-th Tribonacci Number is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.