由给定 N 个整数形成的所有对的成对差的平均值
给定一个包含N个整数的数组arr[] ,任务是计算由给定N个整数形成的两个元素之间的差的平均值。
例子:
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
方法:这个问题可以通过使用贪心方法和前缀和方法来解决。如果数组arr[]中的点按排序顺序排列,则第 i个点到所有较大点的距离之和可以计算为: (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) 。使用此观察,可以使用以下步骤解决给定问题:
- 最初以非递减顺序对数组arr[]进行排序。
- 创建数组arr[]的前缀和数组pre[ ] 。
- 遍历每个索引i并将(pre[N – 1] – pre[i]) – arr[i] * (N – 1 – i)添加到变量ans中。
- 所需的答案是ans / count of pairs => ans / (N*(N-1)/2) 。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find average distance
// between given points on a line
long double averageDistance(
vector arr, int N)
{
// Sorting the array arr[]
sort(arr.begin(), arr.end());
// Stores the prefix sum
// array of arr[]
int pre[N] = { 0 };
pre[0] = arr[0];
// Loop to calculate prefix sum
for (int i = 1; i < N; i++) {
pre[i] = pre[i - 1] + arr[i];
}
// Initialising the answer variable
long double ans = 0;
// Loop to iterate through arr[]
for (int i = 0; i < N - 1; i++) {
// Adding summation of all
// distances from ith point
ans += (pre[N - 1] - pre[i])
- arr[i] * (N - 1 - i);
}
// Return Average
return ans / ((N * (N - 1)) / 2);
}
// Driver Code
int main()
{
vector arr = { -1, 3, -5, 4 };
cout << averageDistance(arr, arr.size());
return 0;
}
Java
// Java implementation for the above approach
import java.util.*;
class GFG {
// Function to find average distance
// between given points on a line
static double averageDistance(int[] arr, int N)
{
// Sorting the array arr[]
Arrays.sort(arr);
// Stores the prefix sum
// array of arr[]
int[] pre = new int[N];
pre[0] = arr[0];
// Loop to calculate prefix sum
for (int i = 1; i < N; i++) {
pre[i] = pre[i - 1] + arr[i];
}
// Initialising the answer variable
double ans = 0;
// Loop to iterate through arr[]
for (int i = 0; i < N - 1; i++) {
// Adding summation of all
// distances from ith point
ans += (pre[N - 1] - pre[i])
- arr[i] * (N - 1 - i);
}
// Return Average
ans = (ans / ((N * (N - 1)) / 2));
return ans;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { -1, 3, -5, 4 };
System.out.print(String.format(
"%.5f", averageDistance(arr, arr.length)));
}
}
// This code is contributed by ukasp.
Python3
# Python3 program for above approach
# Function to find average distance
# between given points on a line
def averageDistance(arr, N):
# Sorting the array arr[]
arr.sort()
# Stores the prefix sum
# array of arr[]
pre = [0 for _ in range(N)]
pre[0] = arr[0]
# Loop to calculate prefix sum
for i in range(1, N):
pre[i] = pre[i - 1] + arr[i]
# Initialising the answer variable
ans = 0
# Loop to iterate through arr[]
for i in range(0, N - 1):
# Adding summation of all
# distances from ith point
ans += ((pre[N - 1] - pre[i]) -
(arr[i] * (N - 1 - i)))
# Return Average
return ans / ((N * (N - 1)) / 2)
# Driver Code
if __name__ == "__main__":
arr = [ -1, 3, -5, 4 ]
print(averageDistance(arr, len(arr)))
# This code is contributed by rakeshsahni
C#
// C# implementation for the above approach
using System;
class GFG
{
// Function to find average distance
// between given points on a line
static double averageDistance(
int []arr, int N)
{
// Sorting the array arr[]
Array.Sort(arr);
// Stores the prefix sum
// array of arr[]
int []pre = new int[N];
pre[0] = arr[0];
// Loop to calculate prefix sum
for (int i = 1; i < N; i++) {
pre[i] = pre[i - 1] + arr[i];
}
// Initialising the answer variable
double ans = 0;
// Loop to iterate through arr[]
for (int i = 0; i < N - 1; i++) {
// Adding summation of all
// distances from ith point
ans += (pre[N - 1] - pre[i])
- arr[i] * (N - 1 - i);
}
// Return Average
ans = Math.Round((ans / ((N * (N - 1)) / 2)), 5);
return ans;
}
// Driver Code
public static void Main()
{
int []arr = { -1, 3, -5, 4 };
Console.Write(averageDistance(arr, arr.Length));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
5.16667
时间复杂度: O(N*log N)
辅助空间: O(1)