Problem · Array
Sum of Max of Subarrays
Learn this problemProblem statement
Find the sum of the maximum of all subarrays multiplied by their length in O(n).
For example, given an array arr = [4,2,1,2], the output should be 59.
Function
sumOfMaxOfSubarrays(arr: int[]) → long
Complete the function sumOfMaxOfSubarrays in the editor.
sumOfMaxOfSubarrays has the following parameter:
int[] arr: an array of integers
Returns
long integer: the sum of the maximum of all subarrays multiplied by their length
Examples
Example 1
arr = [4, 2, 1, 2]return = 59Here's how the sum is calculated:
[4] 1 4 1 * 4 = 4
[4, 2] 2 4 2 * 4 = 8
[4, 2, 1] 3 4 3 * 4 = 12
[4, 2, 1, 2] 4 4 4 * 4 = 16
[2] 1 2 1 * 2 = 2
[2, 1] 2 2 2 * 2 = 4
[2, 1, 2] 3 2 3 * 2 = 6
[1] 1 1 1 * 1 = 1
[1, 2] 2 2 2 * 2 = 4
[2] 1 2 1 * 2 = 2
Sum == 59 ☕️
More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026