Given an array A of n integers. The task is to find the sum of minimum of all possible (contiguous) subarray of A.
Input: A = [3, 1, 2, 4]
Explanation: Subarrays are , , , , [3, 1], [1, 2], [2, 4], [3, 1, 2], [1, 2, 4], [3, 1, 2, 4].
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1. Sum is 17.
Input : A = [1, 2, 3, 4]
Output : 20
Approach: The Naive approach is to generate all possible (contiguous) subarrays, find their minimum and add them to result. The time complexity will be O(N2).
Efficient Approach: The general intuition for solution to the problem is to find sum(A[i] * f(i)), where f(i) is the number of subarrays in which A[i] is the minimum.
In order to find f[i], we need to find out:
left[i], the length of strictly larger numbers on the left of A[i],
right[i], the length of larger numbers on the right of A[i].
We make two arrays left[ ] and right[ ] such that:
left[i] + 1 equals to the number of subarrays ending with A[i], and A[i] is only single minimum.
Similarly, right[i] + 1 equals to the number of subarrays starting with A[i], and A[i] is first minimum.
Finally, f(i) = (left[i]) * (right[i]), where f[i] equals total number of subarrays in which A[i] is minimum.
Below is the implementation of above approach
Time Complexity: O(N), where N is the length of A.
Space Complexity: O(N).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of minimum and maximum elements of all subarrays of size k.
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Minimum LCM of all subarrays of length at least 2
- Number of subarrays whose minimum and maximum are same
- Minimum and Maximum of all subarrays of size K using Map
- Subarrays with distinct elements
- Count subarrays with same even and odd elements
- Sum of minimum element of all subarrays of a sorted array
- Sum of all differences between Maximum and Minimum of increasing Subarrays
- Minimum absolute difference of XOR values of two subarrays
- Count subarrays with all elements greater than K
- Count of subarrays having exactly K distinct elements
- Split array into two subarrays such that difference of their maximum is minimum
- Min difference between maximum and minimum element in all Y size subarrays
- Count subarrays having sum of elements at even and odd positions equal
- Count of subarrays of size K with elements having even frequencies
- Count Subarrays with Consecutive elements differing by 1
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Find number of subarrays ending with arr[i] where arr[i] is the minimum element of that subarray
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.