Sum of Max of Subarrays

Sum of Max of Subarrays

FULLTIME
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.

Complete the function sumOfMaxOfSubarrays
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

Example:
Input: arr = [4, 2, 1, 2]
Output: 59
Explanation:
Here'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



Get one-to-one training from Google Facebook engineers

Top-notch Professionals

Learn from Facebook and Google senior engineers interviewed 100+ candidates.
Most recent interview questions and system design topics gathered from aonecode alumnus.
One-to-one online classes. Get feedbacks from real interviewers.

Customized Private Class

Already a coding expert? - Advance straight to hard interview topics of your interest.
New to the ground? - Develop basic coding skills with your own designated mentor.
Days before interview? - Focus on most important problems in target company question bank.