Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Tribonacci Sequence
MEDIUM
Matrices (2D Arrays)
Greedy
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Matrices (2D Arrays)
-
-
Greedy
-
-
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Tribonacci Sequence

Contributed by
Paritosh Singh
Medium
yellow-spark
0/80
Share
0 upvotes

Problem Statement

You are given an integer ‘N’. Your task is to find out the ‘N’th Tribonacci Number. ‘N’th number in the Tribonacci sequence is defined as Tn = T n - 1 + Tn - 2 + Tn - 3. Where T0 = 0, T1 = 1, T2= 1. Return the answer in mod of 109 + 7.

For Example :
You are given ‘N’ = 5, Here we know 0th, 1st and 2nd Tribonacci numbers are 0, 1 and 1 respectively. Therefore we can calculate the 3rd number as 0 + 1 + 1 = 2 and 4th number as 1 + 1 + 2 = 4 and 5th number as 1 + 2 + 4 = 7. Hence the answer is 7.
Follow Up :
Can you solve it in logarithmic time?
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
5
2
Sample Output 1 :
7
1
Explanation for Sample Input 1:
For the first test case, ‘N’ = 5, Here we know 0th, 1st and 2nd Tribonacci numbers are 0, 1 and 1 respectively. Therefore we can calculate the 3rd number as 0 + 1 + 1 = 2 and 4th number as 1 + 1 + 2 = 4 and 5th number as 1 + 2 + 4 = 7. Hence the answer is 7.

For the second test case, ‘N’ = 2, Here, the second number in the Tribonacci sequence is already defined as 1. Hence the answer is 1.
Sample Input 2 :
2
0
9
Sample Output 2 :
0
81
Reset Code
Full screen
Auto
copy-code
Console