<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 {
    long long amin(vector<int>& arr){
      int n = arr.size();
       stack<long long>s1,s2;
       vector<long long>left(n),right(n);
       long long ans=0;

       for(int i=0;i<n;i++){
           right[i]=n-i-1;
           left[i]=i;
       }

       for(int i=0;i<n;i++){
           while(!s1.empty() && arr[s1.top()]>arr[i]){    // why here equal to sign is not applicable
               right[s1.top()]=i-s1.top()-1;      // it denotes lenght at which next min occur
               s1.pop();
           }
           s1.push(i);
       }

        for(int i=n-1;i>=0;i--){
           while(!s2.empty() && arr[s2.top()]>=arr[i]){     // why here equal to sign is applicable
               left[s2.top()]=s2.top()-i-1;
               s2.pop();
           }
           s2.push(i);
       }

         for(int i=0;i<n;i++){
             ans= (ans +(1ll*arr[i]*(left[i]+1)*(right[i]+1)));
         }
       return ans;  
    }


    long long amax(vector<int>& arr){
      int n = arr.size();
       stack<long long>s1,s2;
       vector<long long>left(n),right(n);
       long long ans=0;

       for(int i=0;i<n;i++){
           right[i]=n-i-1;
           left[i]=i;
       }

       for(int i=0;i<n;i++){
           while(!s1.empty() && arr[s1.top()]<arr[i]){
               right[s1.top()]=i-s1.top()-1;
               s1.pop();
           }
           s1.push(i);
       }

        for(int i=n-1;i>=0;i--){
           while(!s2.empty() && arr[s2.top()]<=arr[i]){
               left[s2.top()]=s2.top()-i-1;
               s2.pop();
           }
           s2.push(i);
       }

         for(int i=0;i<n;i++){
             ans= (ans +(1ll*arr[i]*(left[i]+1)*(right[i]+1)));
         }
       return ans;  
    }
public:
    long long subArrayRanges(vector<int>& nums) {
           
           long long ans1=amin(nums);
           long long ans2=amax(nums);

           return ans2-ans1;
    }
};
15300