给定一个由N个正整数组成的数组arr [] ,任务是找到一个数组元素X ,以使其与每个数组元素的绝对差之和最小。
例子:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 3
Explanation:
- For element arr[0](= 1): |(1 – 1)| + |(2 – 1)| + |(3 – 1)| + |(4 – 1)| + |(5 – 1)| = 0 + 1 + 2 + 3 + 4 = 10.
- For element arr[1](= 2): |(1 – 2)| + |(2 – 2)| + |(3 – 2)| + |(4 – 2)| + |(5 – 2)| = 1 + 0 + 1 + 2 + 3 = 7.
- For element arr[2](= 3): |(1 – 3)| + |(2 – 3)| + |(3 – 3)| + |(4 – 3)| + |(5 – 3)| = 2 + 1 + 0 + 1 + 2 = 6.
- For element arr[3](= 4): |(1 – 4)| + |(2 – 4)| + |(3 – 4)| + |(4 – 4)| + |(5 – 4)| = 3 + 2 + 1 + 0 + 1 = 7.
- For element arr[4](= 5): |(1 – 5)| + |(2 – 5)| + |(3 – 5)| + |(4 – 5)| + |(5 – 5)| = 4 + 3 + 2 + 1 + 0 = 10.
Therefore, the element having minimum sum of absolute differences with all array elements is 3.
Input: arr[] = {15, 12, 13, 10}
Output: 12
天真的方法:解决给定问题的最简单方法是找到数组元素与数组中每个元素一一对应的绝对差之和,并打印出具有较小差和的元素。
时间复杂度: 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)