Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
You’re given an array A[] of dimension N, the duty is to divide the array into precisely three subarrays such that each aspect belongs to precisely one subarray such that the product of the sum of the subarrays is the utmost.
Examples:
Enter: N = 4, A[] = { 1, 2, 2, 3}
Output: 18
Clarification: The optimum partitions are {1, 2}, {2}, {3}Enter: N = 3, A[] = { 3, 5, 7}
Output: 105
Clarification: There is just one potential partition {3}, {5}, {7}.
Strategy: This drawback might be solved utilizing the idea of sliding window and prefixsuffix array.
First, calculate the utmost product of two subarrays contemplating the dimensions of the array from 0 to N ranging from proper. Now as soon as we’ve got the utmost product of two subarrays then the third subarray will likely be on the left aspect.
For instance, if we’ve got calculated the utmost product of two subarrays for all of the arrays ranging from i to N the place 0 < i < N – 1 then the third subarray will likely be subarray from 0 to i. And now we will calculate the utmost product of those two subarrays to get most product of three subarrays.
Comply with the steps talked about beneath to implement the thought.
Under is the implementation of the above strategy:

Time Complexity: O(N)
Auxiliary House: O(N)
[ad_2]