![]() |
VOOZH | about |
Given an array arr[] of length N, the task is to find the sum of the maximum elements of every possible sub-array of the array.
Examples:
Input : arr[] = {1, 3, 1, 7}
Output : 42
Max of all sub-arrays:
{1} - 1
{1, 3} - 3
{1, 3, 1} - 3
{1, 3, 1, 7} - 7
{3} - 3
{3, 1} - 3
{3, 1, 7} - 7
{1} - 1
{1, 7} - 7
{7} - 7
1 + 3 + 3 + 7 + 3 + 3 + 7 + 1 + 7 + 7 = 42
Input : arr[] = {1, 1, 1, 1, 1}
Output : 15
We have already discussed an O(N) approach using stack for this problem in this article.
Approach :
In this article, we will learn how to solve this problem using divide and conquer.
Let's assume that element at ith index is largest of all. For any sub-array that contains index 'i', the element at 'i' will always be maximum in the sub-array.
If element at ith index is largest, we can safely say, that element ith index will be largest in (i+1)*(N-i) subarrays. So, its total contribution will be arr[i]*(i+1)*(N-i). Now, we will divide the array in two parts, (0, i-1) and (i+1, N-1) and apply the same algorithms to both of them separately.
So our general recurrence relation will be:
maxSumSubarray(arr, l, r) = arr[i]*(r-i+1)*(i-l+1) + maxSumSubarray(arr, l, i-1) + maxSumSubarray(arr, i+1, r) where i is index of maximum element in range [l, r].
Now, we need a way to efficiently answer rangeMax() queries. Segment tree will be an efficient way to answer this query. We will need to answer this query at most N times. Thus, the time complexity of our divide and conquer algorithm will O(Nlog(N)).
If we have to answer the problem "Sum of minimum of all subarrays" then we will use the segment tree to answer rangeMin() queries. For this, you can go through the article segment tree range minimum.
Below is the implementation code:
42
Time complexity : O(Nlog(N))