What is the space complexity of the following recursive implementation?

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

What is the space complexity of the following recursive implementation?

Post by answerhappygod »

#include<stdio.h>
#include<limits.h>
int max_of_two(int a, int b)
{
if(a > b)
return a;
return b;
}
int rod_cut(int *prices, int len)
{
int max_price = INT_MIN; // INT_MIN is the min value an integer can take
int i;
if(len <= 0 )
return 0;
for(i = 0; i < len; i++)
// subtract 1 because index starts from 0
max_price = max_of_two(max_price, prices + rod_cut(prices,len - i - 1));
return max_price;
}
int main()
{
int prices[]={2, 5, 6, 9, 9, 17, 17, 18, 20, 22},len_of_rod = 10;
int ans = rod_cut(prices, len_of_rod);
printf("%d",ans);
return 0;
}
a) O(1)
b) O(logn)
c) O(nlogn)
d) O(n!)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!

This topic has 1 reply

You must be a registered member and logged in to view the replies in this topic.


Register Login
 
Post Reply