The Wayback Machine - https://web.archive.org/web/20211222193219/https://www.geeksforgeeks.org/sum-of-absolute-difference-of-maximum-and-minimum-of-all-subarrays/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Sum of absolute difference of maximum and minimum of all subarrays

  • Last Updated : 22 Dec, 2021

Given an array arr containing N integers, the task is to find the sum of the absolute difference of maximum and minimum of all subarrays.

Example:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {1, 4, 3}
Output: 7
Explanation: The following are the six subarrays:
[1] : maximun – minimum= 1 – 1 = 0
[4] : maximun – minimum= 4 – 4 = 0
[3] : maximun – minimum= 3 – 3 = 0
[1, 4] : maximun – minimum= 4 – 1 = 3
[4, 3] : maximun – minimum= 4 – 3 = 1
[1, 4, 3] : maximun – minimum= 4 – 1 = 3
As a result, the total sum is: 0 + 0 + 0 + 3 + 1 + 3 = 7



Input: arr[] = {1, 6, 3}
Output: 13

Approach: The approach is to find all possible subarrays, and maintain their maximum and minimum, then use them to calculate the sum. Now, follow the below step to solve this problem:

  1. Create a variable sum to store the final answer and initialise it to 0.
  2. Run a loop from i=0 to i<N and in each iteration:
    • Create two variables mx and mn to store the maximum and minimum of the subarray respectively.
    • Initialise mx and mn to arr[i].
    • Now, run another loop from j=i to j<N:
      • Each iteration of this loop is used to calculate the maximum and minimum of subarray from i to j.
      • So, change mx to the maximum out of mx and arr[j].
      • And change mn to the minimum of mn and arr[j].
      • Now, add (mx-mn) to the sum.
  3. Return sum as the final answer to this problem.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the sum
// of the absolute difference
// of maximum and minimum of all subarrays
int sumOfDiff(vector<int>& arr)
{
  
    int sum = 0;
    int n = arr.size();
  
    for (int i = 0; i < n; i++) {
        int mn = arr[i];
        int mx = arr[i];
  
        for (int j = i; j < n; j++) {
            mx = max(mx, arr[j]);
            mn = min(mn, arr[j]);
            sum += (mx - mn);
        }
    }
  
    return sum;
}
  
// Driver Code
int main()
{
  
    vector<int> arr = { 1, 6, 3 };
    cout << sumOfDiff(arr);
    return 0;
}
Output
13

Time complexity: O(N2 )
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!