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[] = {1, 2, 3, 4, 5, 7, 9, 11, 14} Output: 188

A **simple solution** for this problem is to one by one look for each pair take their difference and sum up them together. The time complexity for this approach is O(n^{2}).

An **efficient solution** for this problem needs a simple observation. Since array is sorted and elements are distinct, when we take sum of absolute difference of pairs each element in the i’th position is added ‘i’ times and subtracted ‘n-1-i’ times.

For example in {1,2,3,4} element at index 2 is arr[2] = 3 so all pairs having 3 as one element will be (1,3), (2,3) and (3,4), now when we take summation of absolute difference of pairs, then for all pairs in which 3 is present as one element summation will be = (3-1)+(3-2)+(4-3). We can see that 3 is added i = 2 times and subtracted n-1-i = (4-1-2) = 1 times.

The generalized expression for each element will be sum = sum + (i*a[i]) – (n-1-i)*a[i].

## C++

`// C++ program to find sum of absolutre differences ` `// in all pairs in a sorted array of distinct numbers ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate sum of absolute difference ` `// of all pairs in array ` `// arr[] --> array of elements ` `int` `sumPairs(` `int` `arr[],` `int` `n) ` `{ ` ` ` `// final result ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i=n-1; i>=0; i--) ` ` ` `sum += i*arr[i] - (n-1-i)*arr[i]; ` ` ` `return` `sum; ` `} ` ` ` `// Driver program to run the case ` `int` `main() ` `{ ` ` ` `int` `arr[] = {1, 8, 9, 15, 16}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << sumPairs(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of absolutre ` `// differences in all pairs in a sorted ` `// array of distinct numbers ` `class` `GFG { ` ` ` ` ` `// Function to calculate sum of absolute ` ` ` `// difference of all pairs in array ` ` ` `// arr[] --> array of elements ` ` ` `static` `int` `sumPairs(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// final result ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) ` ` ` `sum += i * arr[i] - (n - ` `1` `- i) ` ` ` `* arr[i]; ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main(String arg[]) ` ` ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `8` `, ` `9` `, ` `15` `, ` `16` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.print(sumPairs(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find sum of ` `# absolutre differences in all pairs ` `# in a sorted array of distinct numbers ` ` ` `# Function to calculate sum of absolute ` `# difference of all pairs in array ` `# arr[] --> array of elements ` `def` `sumPairs(arr, n): ` ` ` ` ` `# final result ` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `sum` `+` `=` `i` `*` `arr[i] ` `-` `(n` `-` `1` `-` `i) ` `*` `arr[i] ` ` ` `return` `sum` ` ` `# Driver program ` `arr ` `=` `[` `1` `, ` `8` `, ` `9` `, ` `15` `, ` `16` `] ` `n ` `=` `len` `(arr) ` `print` `(sumPairs(arr, n)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of absolutre ` `// differences in all pairs in a sorted ` `// array of distinct numbers ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to calculate sum of absolute ` ` ` `// difference of all pairs in array ` ` ` `// arr[] --> array of elements ` ` ` `static` `int` `sumPairs(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// final result ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) ` ` ` `sum += i * arr[i] - (n - 1 - i) ` ` ` `* arr[i]; ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = { 1, 8, 9, 15, 16 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.Write(sumPairs(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` ` ` `// PHP program to find sum of absolutre differences ` `// in all pairs in a sorted array of distinct numbers ` ` ` `// Function to calculate sum of absolute difference ` `// of all pairs in array ` `// arr[] --> array of elements ` `function` `sumPairs(` `$arr` `,` `$n` `) ` `{ ` ` ` `// final result ` ` ` `$sum` `= 0; ` ` ` `for` `(` `$i` `=` `$n` `-1; ` `$i` `>=0; ` `$i` `--) ` ` ` `$sum` `= ` `$sum` `+ ` `$i` `*` `$arr` `[` `$i` `] - (` `$n` `-1-` `$i` `)*` `$arr` `[` `$i` `]; ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver program to run the case ` `$arr` `= ` `array` `(1, 8, 9, 15, 16); ` ` ` `$n` `= sizeof(` `$arr` `)/sizeof(` `$arr` `[0]); ` ` ` `echo` `sumPairs(` `$arr` `, ` `$n` `); ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

74

Time Complexity : O(n)

Auxiliary space: O(1)e

**What if array is not sorted?**

The efficient solution is also better for the cases where array is not sorted. We can sort the array first in O(n Log n) time and then find the required value in O(n). So overall time complexity is O(n Log n) which is still better than O(n^{2})

This article is contributed by **Shashank Mishra ( Gullu )**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

## Recommended Posts:

- Sum of absolute differences of pairs from the given array that satisfy the given condition
- Minimum and Maximum sum of absolute differences of pairs
- Array formed using sum of absolute differences of that element with all other elements
- Array element with minimum sum of absolute differences
- Sum of bit differences among all pairs
- Count all disjoint pairs having absolute difference at least K from a given array
- Sum of absolute difference of all pairs raised to power K
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Count of all pairs in an Array with minimum absolute difference
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Length of longest subsequence having absolute difference of all pairs divisible by K
- Minimum sum of absolute difference of pairs of two arrays
- Sum of all differences between Maximum and Minimum of increasing Subarrays
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count pairs in an array whose absolute difference is divisible by K
- Maximize sum of consecutive differences in a circular array
- Clustering/Partitioning an array such that sum of square differences is minimum
- Minimum sum of differences with an element in an array
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Given an absolute sorted array and a number K, find the pair whose sum is K