Company: Amazon Ml school_3aug
Difficulty: medium
Stacking Money Problem Description After a tiring fight against the Mad Titan, Wanda and Vision settle down to a normal life. Now, they have to worry about trivial things like money. One day, Vision was stacking coins. He made N different stacks of coins represented in the form of an integer array A, where each stack had A coins. Vision asked Wanda the following question: Find the sum of the difference of maximum and minimum number of coins over all subarrays of A. Constraints 1 <= N <= 10 5 1 <= A[i] <= 10 9 Input Format The first argument is the integer array A. Output Format Return a single long integer denoting the value Vision asked for. Examples Example 1: Input: A = [1, 2, 3] Output: 4 Explanation: The different subarrays are with their difference in maximum and minimum are: [1] -> (1) - (1) = 0 [2] -> (2) - (2) = 0 [3] -> (3) - (3) = 0 [1, 2] -> (2) - (1) = 1 [2, 3] -> (3) - (2) = 1 [1, 2, 3] -> (3) - (1) = 2 The sum over all subarrays is 4. Exampl