Padovan Sequence
Easy Accuracy: 20.0% Submissions: 145 Points: 2

A Padovan Sequence is a sequence which is represented by the following recurrence relation
P(N) = P(N-2) + P(N-3)
P(0) = P(1) = P(2) = 1
Given a number N, find the Nth number in this sequence.

Note: Since the output may be very large, compute the answer modulus 10^9+7.


Example 1:

Input:
N = 3
Output: 2
Explanation: P1 + P0 = P3
P1 = 1 and P0 = 1


​Example 2:

Input: 
N = 4
Output: 2
Explanation: P2 + P1 = P4
 P2 = 1 and P1 = 1


Your Task:
You don't need to read input or print anything. Your task is to complete the function padovanSequence() which takes integer N as input parameter and return Nth number in Padovan Sequence.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)


Constraints:
1 <= N <= 106

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.