Array ( [0] => [1] => questions [2] => Strings [3] => Fabinocci-Numbers-with-matrix-multiplication ) Popular Algorithms | Fabinocci Numbers with matrix multiplication | THE INQUISITIVE





Fabinocci Numbers with matrix multiplication

LEVEL:Beginner

Description

For the given input print the nth number in the fabinocii series

Input Format

Single line contains n

Output Format

Print the nth number in the fabinocci series


Example 1:

Input
1
Output
0
Example 2:

Input
2
Output
1
Example 3:

Input
3
Output
1

oops

Login to see Discussion




Approach

i)create a method multiply which multiplies the two 2 dimensional arrays and store the values in the first array
ii)create a another method power
iii)if n equal to 0 or 1 just return
iv)else create a 2d array and assign 1 1 1 0 to it respectively
v)call the function power with F and n/2
vi)then multiply F and F matrix
vii)if n is even again multiply F and M

viii)create a method fabinocii matrix
ix)create a 2 d array initialize with 1 1 1 0 call the power function with F and n-1 and return first elemetn of the matrix

x)in main method take the required input and print the output

Time Complexity: O(log n)
Space Complexity: O(1)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution