📌  相关文章
📜  每个数组元素的最小绝对差之和

📅  最后修改于: 2021-04-24 20:09:14             🧑  作者: Mango

给定n个不同整数的数组。问题是找到每个数组元素的最小绝对差之和。对于存在于数组中索引i处的元素x ,其最小绝对差的计算公式如下:
最小绝对差(x) = min(abs(x – arr [j])),其中1 <= j <= n且j!= i,而abs是绝对值。
输入约束: 2 <= n
例子:

Input : arr = {4, 1, 5}
Output : 5
Sum of absolute differences is |4-5| + |1-4| + |5-4|

Input : arr = {5, 10, 1, 4, 8, 7}
Output : 9

Input : {12, 10, 15, 22, 21, 20, 1, 8, 9}
Output : 18

天真的方法:使用两个循环。使用外循环选择一个数组元素,并使用内循环计算其与其余数组元素的绝对差。找到最小绝对值并将其添加到总和中。时间复杂度O(n 2 )。
高效方法:以下步骤是:

  1. 对大小为n的数组进行排序。
  2. 对于数组的第一个元素,使用第二个数组元素计算其最小绝对差。
  3. 对于最后一个数组元素,使用第二个最后一个数组元素计算其最小绝对差。
  4. 对于其余的数组元素1 <= i <= n-2 ,元素在索引i处的最小绝对差的计算公式为: minAbsDiff = min(abs(arr [i] – arr [i-1]), abs(ar [i] – arr [i + 1]))。
C++
// C++ implementation to find the sum of minimum
// absolute difference of each array element
#include 
using namespace std;
 
// function to find the sum of
// minimum absolute difference
int sumOfMinAbsDifferences(int arr[], int n)
{
    // sort the given array
    sort(arr, arr+n);
     
    // initialize sum
    int sum = 0;
     
    // min absolute difference for
    // the 1st array element
    sum += abs(arr[0] - arr[1]);
     
    // min absolute difference for
    // the last array element
    sum += abs(arr[n-1] - arr[n-2]);
     
    // find min absolute difference for rest of the
    // array elements and add them to sum
    for (int i=1; iJava
// java implementation to find the sum
// of minimum absolute difference of
// each array element
import java.*;
import java.util.Arrays;
 
public class GFG {
     
    // function to find the sum of
    // minimum absolute difference
    static int sumOfMinAbsDifferences(
                         int arr[] ,int n)
    {
         
        // sort the given array
        Arrays.sort(arr);
         
        // initialize sum
        int sum = 0;
         
        // min absolute difference for
        // the 1st array element
        sum += Math.abs(arr[0] - arr[1]);
         
        // min absolute difference for
        // the last array element
        sum += Math.abs(arr[n-1] - arr[n-2]);
         
        // find min absolute difference for
        // rest of the array elements and
        // add them to sum
        for (int i = 1; i < n - 1; i++)
            sum +=
            Math.min(Math.abs(arr[i] - arr[i-1]),
                    Math.abs(arr[i] - arr[i+1]));
             
        // required sum
        return sum;
    }    
 
    // Driver code
    public static void main(String args[])
    {
        int arr[] = {5, 10, 1, 4, 8, 7};
        int n = arr.length;
         
        System.out.println( "Sum = "
        + sumOfMinAbsDifferences(arr, n));
    }
}
 
// This code is contributed by Sam007.


Python3
# Python implementation to find the
# sum of minimum absolute difference
# of each array element
 
# function to find the sum of
# minimum absolute difference
 
def sumOfMinAbsDifferences(arr,n):
    # sort the given array
    arr.sort()
    # initialize sum
    sum = 0
         
    # min absolute difference for
    # the 1st array element
    sum += abs(arr[0] - arr[1]);
         
    # min absolute difference for
    # the last array element
    sum += abs(arr[n - 1] - arr[n - 2]);
         
    # find min absolute difference for
    # rest of the array elements and
    # add them to sum
    for i in range(1, n - 1):
        sum += min(abs(arr[i] - arr[i - 1]),
                  abs(arr[i] - arr[i + 1]))
             
    # required sum
    return sum;
         
 
# Driver code
arr = [5, 10, 1, 4, 8, 7]
n = len(arr)
print( "Sum = ", sumOfMinAbsDifferences(arr, n))
     
     
#This code is contributed by Sam007


C#
// C# implementation to find the sum
// of minimum absolute difference of
// each array element
using System;
         
public class GFG {
     
    // function to find the sum of
    // minimum absolute difference
    static int sumOfMinAbsDifferences(
                         int []arr ,int n)
    {
         
        // sort the given array
        Array.Sort(arr);
         
        // initialize sum
        int sum = 0;
         
        // min absolute difference for
        // the 1st array element
        sum += Math.Abs(arr[0] - arr[1]);
         
        // min absolute difference for
        // the last array element
        sum += Math.Abs(arr[n-1] - arr[n-2]);
         
        // find min absolute difference for
        // rest of the array elements and
        // add them to sum
        for (int i = 1; i < n - 1; i++)
            sum +=
            Math.Min(Math.Abs(arr[i] - arr[i-1]),
                    Math.Abs(arr[i] - arr[i+1]));
             
        // required sum
        return sum;
    }    
 
    // Driver code
    public static void Main ()
    {
        int []arr = {5, 10, 1, 4, 8, 7};
        int n = arr.Length;
         
        Console.Write( "Sum = "
        + sumOfMinAbsDifferences(arr, n));
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


输出:

Sum = 9