📌  相关文章
📜  具有最小绝对差之和的数组元素|套装2

📅  最后修改于: 2021-05-17 04:46:20             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是找到一个数组元素X ,以使其与每个数组元素的绝对差之和最小。

例子:

天真的方法:解决给定问题的最简单方法是找到数组元素与数组中每个元素一一对应的绝对差之和,并打印出具有较小差和的元素。
时间复杂度: O(N 2 )
辅助空间: O(1)

基于中位数的方法:请参考本文的上一篇文章,以使用中位数查找技术解决此问题。
时间复杂度: O(NlogN)
辅助空间: O(1)

高效方法:还可以通过最小化每个数组元素X(所有数组元素之和– X * N)的值并找到结果元素X来优化上述方法。请按照以下步骤解决问题:

  • 初始化一个变量,将res表示arr [0] ,该变量存储所得数组元素,其数组元素与res的绝对差之和最小。
  • 找到数组元素的总和并将其存储在变量中,例如sum
  • 初始化一个变量,说minDiff作为sum的值。
  • 遍历给定阵列ARR [],如果的绝对值(总和- (ARR [I] * N))小于MINDIFF然后更新MINDIFF该数值和RES作为当前的数组元素即ARR [i]中
  • 完成上述步骤后,将res的值打印为结果数组元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the element with
// minimum sum of differences between
// any elements in the array
int minimumDiff(int arr[], int N)
{
    // Stores the required X and
    // sum of absolute differences
    int res = arr[0], sum = 0;
 
    // Calculate sum of array elements
    for (int i = 0; i < N; i++)
        sum += arr[i];
 
    // The sum of absolute differences
    // can't be greater than sum
    int min_diff = sum;
 
    // Update res that gives
    // the minimum sum
    for (int i = 0; i < N; i++) {
 
        // If the current difference
        // is less than the previous
        // difference
        if (abs(sum - (arr[i] * N))
            < min_diff) {
 
            // Update min_diff and res
            min_diff = abs(sum - (arr[i] * N));
            res = arr[i];
        }
    }
 
    // Print the resultant value
    cout << res;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    minimumDiff(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find the element with
// minimum sum of differences between
// any elements in the array
static void minimumDiff(int[] arr, int N)
{
     
    // Stores the required X and
    // sum of absolute differences
    int res = arr[0], sum = 0;
 
    // Calculate sum of array elements
    for(int i = 0; i < N; i++)
        sum += arr[i];
 
    // The sum of absolute differences
    // can't be greater than sum
    int min_diff = sum;
 
    // Update res that gives
    // the minimum sum
    for(int i = 0; i < N; i++)
    {
         
        // If the current difference
        // is less than the previous
        // difference
        if (Math.abs(sum - (arr[i] * N)) < min_diff)
        {
             
            // Update min_diff and res
            min_diff = Math.abs(sum - (arr[i] * N));
            res = arr[i];
        }
    }
 
    // Print the resultant value
    System.out.println(res);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = arr.length;
     
    minimumDiff(arr, N);
}
}
 
// This code is contributed by subham348


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the element with
// minimum sum of differences between
// any elements in the array
static void minimumDiff(int[] arr, int N)
{
     
    // Stores the required X and
    // sum of absolute differences
    int res = arr[0], sum = 0;
 
    // Calculate sum of array elements
    for(int i = 0; i < N; i++)
        sum += arr[i];
 
    // The sum of absolute differences
    // can't be greater than sum
    int min_diff = sum;
 
    // Update res that gives
    // the minimum sum
    for(int i = 0; i < N; i++)
    {
         
        // If the current difference
        // is less than the previous
        // difference
        if (Math.Abs(sum - (arr[i] * N)) < min_diff)
        {
             
            // Update min_diff and res
            min_diff = Math.Abs(sum - (arr[i] * N));
            res = arr[i];
        }
    }
 
    // Print the resultant value
    Console.Write(res);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 2, 3, 4, 5 };
    int N = arr.Length;
     
    minimumDiff(arr, N);
}
}
 
// This code is contributed by subham348


Python3
# python 3 program for the above approach
 
# Function to find the element with
# minimum sum of differences between
# any elements in the array
def minimumDiff(arr, N):
   
    # Stores the required X and
    # sum of absolute differences
    res = arr[0]
    sum1 = 0
 
    # Calculate sum of array elements
    for i in range(N):
        sum1 += arr[i]
 
    # The sum of absolute differences
    # can't be greater than sum
    min_diff = sum1
 
    # Update res that gives
    # the minimum sum
    for i in range(N):
       
        # If the current difference
        # is less than the previous
        # difference
        if (abs(sum1 - (arr[i] * N)) < min_diff):
           
            # Update min_diff and res
            min_diff = abs(sum1 - (arr[i] * N))
            res = arr[i]
 
    # Print the resultant value
    print(res)
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5]
    N = len(arr)
    minimumDiff(arr, N)
     
    # This code is contributed by SURENDRA_GANGWAR.


输出:
3

时间复杂度: O(N)
辅助空间: O(1)