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

📅  最后修改于: 2021-09-06 17:43:38             🧑  作者: 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[] ,如果(sum – (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.


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live