Array ( [0] => [1] => questions [2] => Strings [3] => Fabinocci-Numbers-with-matrix-multiplication )
For the given input print the nth number in the fabinocii series
Single line contains n
Print the nth number in the fabinocci series
1
0
2
1
3
1
Login to see Discussion
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.
Login to see Solution