Given an array arr[] containing N integers, the task is to calculate the average of the difference between both elements in pairs formed from given N integers.
Examples:
Input: arr[] = {-1, 3, -5, 4}
Output: 5.166667
Explanation: There are 6 possible pair of points in the given array with the pairwise difference as: diff(-1, 3) = 4, diff(-1, -5) = 4, diff(-1, 4) = 5, diff(3, -5) = 8, diff(3, 4) = 1, diff(-5, 4) = 9. Therefore, average pairwise difference is (4 + 4 + 5 + 8 + 1 + 9)/6 = 31/6 = 5.166667.
Input: arr[] = { -1, 2, -3, 7, -6 }
Output: 6.2
Approach: This problem can be solved by using the Greedy Approach and prefix sum method. If the points in the array arr[] are in sorted order, then the sum of distances of ith point to all the greater points can be calculated as: (arr[i+1] – arr[i]) + (arr[i+2] – arr[i]) … + (arr[N-1] – arr[i]) => (arr[i+1] + arr[i+2]… + arr[N-1]) – arr[i] * (N – 1 – i). Using this observation, the given problem can be solved using the following steps:
- Initially sort the array arr[] in non-decreasing order.
- Create a prefix sum array pre[] of the array arr[].
- Iterate through every index i and add (pre[N – 1] – pre[i]) – arr[i] * (N – 1 – i) into a variable ans.
- The required answer is ans / count of pairs => ans / (N*(N-1)/2).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long double averageDistance(
vector< int > arr, int N)
{
sort(arr.begin(), arr.end());
int pre[N] = { 0 };
pre[0] = arr[0];
for ( int i = 1; i < N; i++) {
pre[i] = pre[i - 1] + arr[i];
}
long double ans = 0;
for ( int i = 0; i < N - 1; i++) {
ans += (pre[N - 1] - pre[i])
- arr[i] * (N - 1 - i);
}
return ans / ((N * (N - 1)) / 2);
}
int main()
{
vector< int > arr = { -1, 3, -5, 4 };
cout << averageDistance(arr, arr.size());
return 0;
}
|
Java
import java.util.*;
class GFG {
static double averageDistance( int [] arr, int N)
{
Arrays.sort(arr);
int [] pre = new int [N];
pre[ 0 ] = arr[ 0 ];
for ( int i = 1 ; i < N; i++) {
pre[i] = pre[i - 1 ] + arr[i];
}
double ans = 0 ;
for ( int i = 0 ; i < N - 1 ; i++) {
ans += (pre[N - 1 ] - pre[i])
- arr[i] * (N - 1 - i);
}
ans = (ans / ((N * (N - 1 )) / 2 ));
return ans;
}
public static void main(String[] args)
{
int [] arr = { - 1 , 3 , - 5 , 4 };
System.out.print(String.format(
"%.5f" , averageDistance(arr, arr.length)));
}
}
|
Python3
def averageDistance(arr, N):
arr.sort()
pre = [ 0 for _ in range (N)]
pre[ 0 ] = arr[ 0 ]
for i in range ( 1 , N):
pre[i] = pre[i - 1 ] + arr[i]
ans = 0
for i in range ( 0 , N - 1 ):
ans + = ((pre[N - 1 ] - pre[i]) -
(arr[i] * (N - 1 - i)))
return ans / ((N * (N - 1 )) / 2 )
if __name__ = = "__main__" :
arr = [ - 1 , 3 , - 5 , 4 ]
print (averageDistance(arr, len (arr)))
|
C#
using System;
class GFG
{
static double averageDistance(
int []arr, int N)
{
Array.Sort(arr);
int []pre = new int [N];
pre[0] = arr[0];
for ( int i = 1; i < N; i++) {
pre[i] = pre[i - 1] + arr[i];
}
double ans = 0;
for ( int i = 0; i < N - 1; i++) {
ans += (pre[N - 1] - pre[i])
- arr[i] * (N - 1 - i);
}
ans = Math.Round((ans / ((N * (N - 1)) / 2)), 5);
return ans;
}
public static void Main()
{
int []arr = { -1, 3, -5, 4 };
Console.Write(averageDistance(arr, arr.Length));
}
}
|
Javascript
<script>
function averageDistance(
arr, N)
{
arr.sort( function (a, b) { return a - b })
let pre = new Array(N).fill(0);
pre[0] = arr[0];
for (let i = 1; i < N; i++) {
pre[i] = pre[i - 1] + arr[i];
}
let ans = 0;
for (let i = 0; i < N - 1; i++) {
ans += (pre[N - 1] - pre[i])
- arr[i] * (N - 1 - i);
}
return ans / ((N * (N - 1)) / 2);
}
let arr = [-1, 3, -5, 4];
document.write(averageDistance(arr, arr.length).toPrecision(6));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)