<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p>

<p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p>

<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0 
[2], range = 2 - 2 = 0
[3], range = 3 - 3 = 0
[1,2], range = 2 - 1 = 1
[2,3], range = 3 - 2 = 1
[1,2,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0
[3], range = 3 - 3 = 0
[3], range = 3 - 3 = 0
[1,3], range = 3 - 1 = 2
[3,3], range = 3 - 3 = 0
[1,3,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,-2,-3,4,1]
<strong>Output:</strong> 59
<strong>Explanation:</strong> The sum of all subarray ranges of nums is 59.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>

class Solution {
public:
    long long subArrayRanges(vector<int>& arr) {

        int n = arr.size();

        // find next smaller on next and right side

        vector<int> left_smaller(n, -1);

        vector<int> right_smaller(n, n);

        stack<int> st;

        // fill left_smaller array

        // find next smaller element on left side

        for (int i = 0; i < n; i++) {
            // remove the index with greater element than arr[i]

            while (!st.empty() && arr[st.top()] >= arr[i]) {
                st.pop();
            }

            // if stack is empty

            if (st.empty()) {
                left_smaller[i] = -1;
            } else {
                left_smaller[i] = st.top();
            }

            // push the current index into stack

            st.push(i);
        }

        while (!st.empty()) {
            st.pop();
        }

        // fill the right_smaller array

        // find the next smaller element on right side

        for (int i = n - 1; i >= 0; i--) {
            // remove the index with greater element than arr[i]

            while (!st.empty() && arr[st.top()] > arr[i]) {
                st.pop();
            }

            // if stack is empty

            if (st.empty()) {
                right_smaller[i] = n;
            } else {
                right_smaller[i] = st.top();
            }

            // push the current index into stack

            st.push(i);
        }

        // find next greater on left and right side

        // find left_greater && right_greater

        vector<int> left_greater(n, -1);

        vector<int> right_greater(n, n);

        while (!st.empty()) {
            st.pop();
        }

        // fill left_greater array

        // find next greater element on left side

        for (int i = 0; i < n; i++) {
            // remove the index with smaller element than arr[i]

            while (!st.empty() && arr[st.top()] <= arr[i]) {
                st.pop();
            }

            // if stack is empty

            if (st.empty()) {
                left_greater[i] = -1;
            } else {
                left_greater[i] = st.top();
            }

            // push the current index into stack

            st.push(i);
        }

        while (!st.empty()) {
            st.pop();
        }

        // fill the right_greater array

        // find the next greater element on right side

        for (int i = n - 1; i >= 0; i--) {
            // remove the index with greater element than arr[i]

            while (!st.empty() && arr[st.top()] < arr[i]) {
                st.pop();
            }

            // if stack is empty

            if (st.empty()) {
                right_greater[i] = n;
            } else {
                right_greater[i] = st.top();
            }

            // push the current index into stack

            st.push(i);
        }

        // find the sum of diff of maximum and minimum

        long long sum = 0;

        for (int i = 0; i < n; i++) {
            long long min_sum = (long long)(right_smaller[i] - i) *
                                (long long)(i - left_smaller[i]) *
                                (long long)arr[i];

            long long max_sum = (long long)(right_greater[i] - i) *
                                (long long)(i - left_greater[i]) *
                                (long long)arr[i];

            sum += (max_sum - min_sum);
        }

        return sum;
    }
};
14100