📜  给定数组中所有对的绝对差之和

📅  最后修改于: 2022-05-13 01:57:52.309000             🧑  作者: Mango

给定数组中所有对的绝对差之和

给定一个不同元素的排序数组,任务是找到给定数组中所有对的绝对差的总和。
例子:

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

解决这个问题的一个简单方法是逐一查看每一对,然后将它们的差异汇总在一起。这种方法的时间复杂度是 O(n 2 )。
这个问题的有效解决方案需要一个简单的观察。由于数组是排序的并且元素是不同的,当我们取对的绝对差的总和时,第 i 个位置的每个元素被添加“i”次并减去“n-1-i”次。
例如,在索引 2 处的 {1,2,3,4} 元素是 arr[2] = 3 所以所有以 3 作为一个元素的对将是 (1,3)、(2,3) 和 (3,4) ,现在当我们对对的绝对差求和时,对于其中 3 作为一个元素存在的所有对,总和将为 = (3-1)+(3-2)+(4-3)。我们可以看到 3 加了 i = 2 次,减去了 n-1-i = (4-1-2) = 1 次。
每个元素的通用表达式为 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
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;
}


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.


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.


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.


PHP
 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);
?>


Javascript


输出:

74