Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE HELP COMPILE AND PLEASE PROVIDE SCREENSHOT. THANKS class fibonacci { // Recursive function to find the nth fibonacci number static long fib(int n) {

PLEASE HELP COMPILE AND PLEASE PROVIDE SCREENSHOT. THANKS

class fibonacci { // Recursive function to find the nth fibonacci number static long fib(int n) { // Base Case if (n <= 1) return n; // Recursive call return fib(n - 1) + fib(n - 2); } public static void main(String args[]) { int arr[]; arr = new int[3]; arr[0]=5; arr[1]=8; arr[2]=10; //arr contains value of n given in question i.e. 10,50,70 for (int i = 0; i < 1; i++) { System.out.print(fib(arr[i]) + " "); } } } Time Complexity : O(2^N)

Space Complexity : O(N)

And this is the dynamic programming approach.

class fibonacci { // dynamic programming method to find the nth fibonacci number. static long fib(int n) { long dp[]= new long[n+1]; dp[0]=0; dp[1]=1; for(int i = 2; i <= n; i++){ dp[i]=dp[i-1] + dp[i-2]; } return dp[n]; } public static void main(String args[]) { int arr[]; arr = new int[3]; arr[0]=10; arr[1]=50; arr[2]=70; //arr contains value of n given in question i.e. 10,50,70 for (int i = 0; i < 3; i++) { System.out.print(fib(arr[i]) + " "); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions

Question

Explain the alkaline nature of aqueous solution of making soda.

Answered: 1 week ago

Question

Comment on the pH value of lattice solutions of salts.

Answered: 1 week ago