通过将对递减和递增 1 来最小化所有数组元素对之间的绝对差之和
给定一个由N个整数组成的数组arr[] (从 1 开始的索引),任务是通过将任意一对元素递减和递增1任意次数来找到所有数组元素对之间的绝对差的最小和。
例子:
Input: arr[] = {1, 2, 3}
Output: 0
Explanation:
Modify the array elements by performing the following operations:
- Choose the pairs of element (arr[1], arr[3]) and incrementing and decrementing the pairs modifies the array to {2, 2, 2}.
After the above operations, the sum of the absolute differences is |2 – 2| + |2 – 2| + |2 – 2| = 0. Therefore, print 0.
Input: arr[] = {0, 1, 0, 1}
Output: 4
方法:给定的问题可以通过使用贪婪方法来解决。可以看出,为了最小化每对数组元素arr[]之间的绝对差之和,使每个数组元素彼此接近的想法。请按照以下步骤解决问题:
- 找到数组元素arr[]的总和并将其存储在一个变量中,比如sum 。
- 现在,如果sum % N的值为0 ,则打印0 ,因为可以使所有数组元素相等,并且表达式的结果值始终为 0 。否则,找到sum % N的值并将其存储在变量中,例如R 。
- 现在,如果所有数组元素都是sum/N ,那么我们可以将数组元素的R个数设为1 ,将其余数组元素设为0 ,以最小化结果值。
- 在上述步骤之后,绝对差的最小和由R*(N – R)给出。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum value
// of the sum of absolute difference
// between all pairs of arrays
int minSumDifference(int ar[], int n)
{
// Stores the sum of array elements
int sum = 0;
// Find the sum of array element
for (int i = 0; i < n; i++)
sum += ar[i];
// Store the value of sum%N
int rem = sum % n;
// Return the resultant value
return rem * (n - rem);
}
// Driver Code
int main()
{
int arr[] = { 3, 6, 8, 5, 2,
1, 11, 7, 10, 4 };
int N = sizeof(arr) / sizeof(int);
cout << minSumDifference(arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to find the minimum value
// of the sum of absolute difference
// between all pairs of arrays
public static int minSumDifference(int ar[], int n) {
// Stores the sum of array elements
int sum = 0;
// Find the sum of array element
for (int i = 0; i < n; i++)
sum += ar[i];
// Store the value of sum%N
int rem = sum % n;
// Return the resultant value
return rem * (n - rem);
}
// Driver Code
public static void main(String args[]) {
int[] arr = { 3, 6, 8, 5, 2, 1, 11, 7, 10, 4 };
int N = arr.length;
System.out.println(minSumDifference(arr, N));
}
}
// This code is contributed by gfgking.
Python3
# Python 3 program for the above approach
# Function to find the minimum value
# of the sum of absolute difference
# between all pairs of arrays
def minSumDifference(ar, n):
# Stores the sum of array elements
sum = 0
# Find the sum of array element
for i in range(n):
sum += ar[i]
# Store the value of sum%N
rem = sum % n
# Return the resultant value
return rem * (n - rem)
# Driver Code
if __name__ == '__main__':
arr = [3, 6, 8, 5, 2, 1, 11, 7, 10, 4]
N = len(arr)
print(minSumDifference(arr, N))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum value
// of the sum of absolute difference
// between all pairs of arrays
public static int minSumDifference(int[] ar, int n)
{
// Stores the sum of array elements
int sum = 0;
// Find the sum of array element
for(int i = 0; i < n; i++)
sum += ar[i];
// Store the value of sum%N
int rem = sum % n;
// Return the resultant value
return rem * (n - rem);
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 6, 8, 5, 2,
1, 11, 7, 10, 4 };
int N = arr.Length;
Console.Write(minSumDifference(arr, N));
}
}
// This code is contributed by sanjoy_62
Javascript
输出:
21
时间复杂度: O(N)
辅助空间: O(1)