What is the time complexity of the following dynamic programming implementation of the maximum sum rectangle problem?
int max_sum_rectangle(int arr[][3],int row,int col)
{
int left, right, tmp[row], mx_sm = INT_MIN, idx, val;
for(left = 0; left < col; left++)
{
for(right = left; right < col; right++)
{
if(right == left)
{
for(idx = 0; idx < row; idx++)
tmp[idx] = arr[idx][right];
}
else
{
for(idx = 0; idx < row; idx++)
tmp[idx] += arr[idx][right];
}
val = kadane_algo(tmp,row);
if(val > mx_sm)
mx_sm = val;
}
}
return mx_sm;
}
int max_sum_rectangle(int arr[][3],int row,int col)
{
int left, right, tmp[row], mx_sm = INT_MIN, idx, val;
for(left = 0; left < col; left++)
{
for(right = left; right < col; right++)
{
if(right == left)
{
for(idx = 0; idx < row; idx++)
tmp[idx] = arr[idx][right];
}
else
{
for(idx = 0; idx < row; idx++)
tmp[idx] += arr[idx][right];
}
val = kadane_algo(tmp,row);
if(val > mx_sm)
mx_sm = val;
}
}
return mx_sm;
}A. O(row*col)
B. O(row)
C. O(col)
D. O(row*col*col)
Answer: Option D
What is the main principle behind Dynamic Programming (DP)?
A. Recursion with memoization.
B. Greedy approach.
C. Divide and conquer.
D. Overlapping subproblems and optimal substructure.
Which of the following problems can be solved using Dynamic Programming?
A. Binary Search
B. Depth-First Search
C. Longest Common Subsequence
D. Quick Sort
What is the key advantage of using Dynamic Programming over plain recursion?
A. It makes the code simpler.
B. It reduces the time complexity by storing results of subproblems.
C. It uses more memory.
D. It makes the code simpler.
In the context of Dynamic Programming, what does the term "memoization" refer to?
A. Using a stack to manage function calls.
B. A method for dividing problems.
C. Storing intermediate results to avoid redundant calculations.
D. A technique to speed up sorting.

Join The Discussion