Avg CTC: 18lpa
Location: Bangalore, Chennai
Job Roles: Java Developer, Software Engineers
Job Link: Software Engineering Roles
Crayon Box is a repeated Question. Previously asked for Sprinklr. Checkout the solution below.
Solution Link Here: Crayon Box
N
. The array's power represents the sum of the values you obtain after performing the said operations for N
times. Our task is to delete the array by performing these operations and print the maximum value of the power that can be obtained after the array is completely deleted.Here, first, we have to find out the maximum value for every i
and j
, i.e., for every subarray, and store that in an array so that we can get the maximum value in O(1)
time, and after that, we have to perform Dynamic Programming.
For the DP approach, we have two choices, either to take the first or the last element, so simply, we will recur for both the first and last element and return the maximum of both.
int x = ((a[i] * turn) + maxarr[i][j]) + func(i + 1, j, a, turn + 1);
int y = ((a[j] * turn) + maxarr[i][j]) + func(i, j - 1, a, turn + 1);
return dp[i][j] = max(x, y);
Time Complexity: O(N^2), N
is the size of the array.