Sum of absolute difference of maximum and minimum of all subarrays
Last Updated :
04 Jan, 2022
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:
Input: arr[] = {1, 4, 3}
Output: 7
Explanation: The following are the six subarrays:
[1] : maximum - minimum= 1 - 1 = 0
[4] : maximum - minimum= 4 - 4 = 0
[3] : maximum - minimum= 3 - 3 = 0
[1, 4] : maximum - minimum= 4 - 1 = 3
[4, 3] : maximum - minimum= 4 - 3 = 1
[1, 4, 3] : maximum - 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:
- Create a variable sum to store the final answer and initialise it to 0.
- 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.
- 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;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the sum
// of the absolute difference
// of maximum and minimum of all subarrays
static int sumOfDiff(int []arr)
{
int sum = 0;
int n = arr.length;
for (int i = 0; i < n; i++) {
int mn = arr[i];
int mx = arr[i];
for (int j = i; j < n; j++) {
mx = Math.max(mx, arr[j]);
mn = Math.min(mn, arr[j]);
sum += (mx - mn);
}
}
return sum;
}
// Driver Code
public static void main(String args[])
{
int []arr = { 1, 6, 3 };
System.out.println(sumOfDiff(arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Function to find the sum
# of the absolute difference
# of maximum and minimum of all subarrays
def sumOfDiff(arr):
sum = 0
n = len(arr)
for i in range(n):
mn = arr[i]
mx = arr[i]
for j in range(i, n):
mx = max(mx, arr[j])
mn = min(mn, arr[j])
sum += (mx - mn)
return sum
# Driver Code
arr = [1, 6, 3]
print(sumOfDiff(arr))
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the sum of the absolute
// difference of maximum and minimum of all
// subarrays
static int sumOfDiff(int []arr)
{
int sum = 0;
int n = arr.Length;
for(int i = 0; i < n; i++)
{
int mn = arr[i];
int mx = arr[i];
for(int j = i; j < n; j++)
{
mx = Math.Max(mx, arr[j]);
mn = Math.Min(mn, arr[j]);
sum += (mx - mn);
}
}
return sum;
}
// Driver Code
public static void Main()
{
int []arr = { 1, 6, 3 };
Console.Write(sumOfDiff(arr));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the sum
// of the absolute difference
// of maximum and minimum of all subarrays
function sumOfDiff(arr) {
let sum = 0;
let n = arr.length;
for (let i = 0; i < n; i++) {
let mn = arr[i];
let mx = arr[i];
for (let j = i; j < n; j++) {
mx = Math.max(mx, arr[j]);
mn = Math.min(mn, arr[j]);
sum += (mx - mn);
}
}
return sum;
}
// Driver Code
let arr = [1, 6, 3];
document.write(sumOfDiff(arr));
// This code is contributed by Potta Lokesh
</script>
Time complexity: O(N2 )
Auxiliary Space: O(1)
Similar Reads
Minimum and Maximum sum of absolute differences of pairs Given an array of N integers where N is even, find the minimum and maximum sum of absolute difference of N/2 pairs formed by pairing every element with one other element. Examples: Input: a[] = {10, -10, 20, -40} Output: min_sum = 40, max_sum = 80 Explanation: Pairs selected for minimum sum (-10, -4
8 min read
Maximum absolute difference between sum of subarrays of size K Given an array arr[] of size N and an integer K, the task is to find maximum absolute difference between the sum of subarrays of size K.Examples : Input: arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}, K = 3 Output: 6 Explanation:: Sum of subarray (-2, -3, 4) = -1 Sum of subarray (-3, 4, -1) = 0 Sum of subar
13 min read
Sum of minimum and maximum elements of all subarrays of size k. Given an array of both positive and negative integers, the task is to compute sum of minimum and maximum elements of all sub-array of size k.Examples: Input : arr[] = {2, 5, -1, 7, -3, -1, -2} K = 4Output : 18Explanation : Subarrays of size 4 are : {2, 5, -1, 7}, min + max = -1 + 7 = 6 {5, -1, 7, -3
15+ min read
Maximum sum of absolute difference of any permutation Given an array, we need to find the maximum sum of the absolute difference of any permutation of the given array. Examples: Input : { 1, 2, 4, 8 } Output : 18 Explanation : For the given array there are several sequence possible like : {2, 1, 4, 8} {4, 2, 1, 8} and some more. Now, the absolute diffe
14 min read
Sum of absolute differences of all pairs in a given array Given a sorted array of distinct elements, the task is to find the summation of absolute differences of all pairs in the given array. Examples: Input : arr[] = {1, 2, 3, 4} Output: 10 Sum of |2-1| + |3-1| + |4-1| + |3-2| + |4-2| + |4-3| = 10 Input : arr[] = {1, 8, 9, 15, 16} Output: 74 Input : arr[]
11 min read