Question
#include<stdio.h>
#include<limits.h>
int rod_cut(int *prices, int len)
{
int max_val[len + 1];
int i,j,tmp_price,tmp_idx;
max_val[0] = 0;
for(i = 1; i <= len; i++)
{
int tmp_max = INT_MIN; // minimum value an integer can hold
for(j = 1; j <= i; j++)
{
tmp_idx = i - j;
tmp_price = prices[j-1] + max_val[tmp_idx];//subtract 1 because index of prices starts from 0
if(tmp_price > tmp_max)
tmp_max = tmp_price;
}
max_val[i] = tmp_max;
}
return max_val[len];
}
int main()
{
int prices[]={2, 5, 6, 9, 9, 17, 17, 18, 20, 22},len_of_rod = 5;
int ans = rod_cut(prices, len_of_rod);
printf("%d",ans);
return 0;
}
a.
12
b.
27
c.
10
d.
17
Posted under Data Structures and Algorithms
Engage with the Community - Add Your Comment
Confused About the Answer? Ask for Details Here.
Know the Explanation? Add it Here.
Q. What is the value stored in max_val[5] after the following program is executed?
Similar Questions
Discover Related MCQs
Q. You are given an array of elements where each array element represents the MAXIMUM number of jumps that can be made in the forward direction from that element. You have to find the minimum number of jumps that are required to reach the end of the array. Which of these methods can be used to solve the problem?
View solution
Q. Consider the following array:
{1, 3, 5, 8, 9, 2, 6, 7, 6}
What is the minimum number of jumps required to reach the end of the array?
View solution
Q. For a given array, there can be multiple ways to reach the end of the array using minimum number of jumps.
View solution
Q. For any array, given that at most one element is non-zero, it is ALWAYS possible to reach the end of the array using minimum jumps.
View solution
Q. The Knapsack problem is an example of ____________
View solution
Q. Which of the following methods can be used to solve the Knapsack problem?
View solution
Q. You are given a knapsack that can carry a maximum weight of 60. There are 4 items with weights {20, 30, 40, 70} and values {70, 80, 90, 200}. What is the maximum value of the items you can carry using the knapsack?
View solution
Q. Which of the following problems is equivalent to the 0-1 Knapsack problem?
View solution
Q. What is the time complexity of the brute force algorithm used to solve the Knapsack problem?
View solution
Q. The 0-1 Knapsack problem can be solved using Greedy algorithm.
View solution
Q. Which of the following methods can be used to solve the matrix chain multiplication problem?
View solution
Q. Which of the following is the recurrence relation for the matrix chain multiplication problem where mat[i-1] * mat[i] gives the dimension of the ith matrix?
View solution
Q. Consider the two matrices P and Q which are 10 x 20 and 20 x 30 matrices respectively. What is the number of multiplications required to multiply the two matrices?
View solution
Q. Consider the matrices P, Q and R which are 10 x 20, 20 x 30 and 30 x 40 matrices respectively. What is the minimum number of multiplications required to multiply the three matrices?
View solution
Q. Consider the matrices P, Q, R and S which are 20 x 15, 15 x 30, 30 x 5 and 5 x 40 matrices respectively. What is the minimum number of multiplications required to multiply the four matrices?
View solution
Q. Consider the brute force implementation in which we find all the possible ways of multiplying the given set of n matrices. What is the time complexity of this implementation?
View solution
Q. Which of the following methods can be used to solve the longest common subsequence problem?
View solution
Q. Consider the strings “PQRSTPQRS” and “PRATPBRQRPS”. What is the length of the longest common subsequence?
View solution
Q. Which of the following problems can be solved using the longest subsequence problem?
View solution
Q. Longest common subsequence is an example of ____________
View solution
Suggested Topics
Are you eager to expand your knowledge beyond Data Structures and Algorithms? We've curated a selection of related categories that you might find intriguing.
Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!