Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1
Last Updated :
06 Aug, 2021
Given an array arr[] ( 1-based indexing ) consisting of N integers, the task is to find the minimum sum of the absolute difference between all pairs of array elements by decrementing and incrementing any pair of elements by 1 any number of times.
Examples:
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
Approach: The given problem can be solved by using a Greedy Approach. It can be observed that to minimize the sum of the absolute difference between every pair of array elements arr[], the idea to make every array element closed to each other. Follow the steps below to solve the problem:
- Find the sum of the array elements arr[] and store it in a variable, say sum.
- Now, if the value of sum % N is 0, then print 0 as all the array elements can be made equal and the resultant value of the expression is always 0. Otherwise, find the value of sum % N and store it in a variable, say R.
- Now, if all the array elements are sum/N, then we can make R number of array elements as 1 and the rest of the array elements as 0 to minimize the resultant value.
- After the above steps, the minimum sum of the absolute difference is given by R*(N - R).
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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
<script>
// JavaScript program for the above approach
// Function to find the minimum value
// of the sum of absolute difference
// between all pairs of arrays
function minSumDifference(ar, n) {
// Stores the sum of array elements
let sum = 0;
// Find the sum of array element
for (let i = 0; i < n; i++) sum += ar[i];
// Store the value of sum%N
let rem = sum % n;
// Return the resultant value
return rem * (n - rem);
}
// Driver Code
let arr = [3, 6, 8, 5, 2, 1, 11, 7, 10, 4];
let N = arr.length;
document.write(minSumDifference(arr, N));
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize absolute difference between the smallest and largest array elements by minimum increment decrement operations Given an array arr[] consisting of N positive integers, the task is to minimize the number of operations required to minimize the absolute difference between the smallest and largest elements present in the array. In each operation, subtract 1 from an array element and increment 1 to another array e
8 min read
Minimize difference between maximum and minimum element by decreasing and increasing Array elements by 1 Given an array arr[], consisting of N positive integers. The task is to minimize the difference between the maximum and the minimum element of the array by performing the below operation any number of times (possibly zero). In one operation choose 2 different index, i and j and decrement arr[i] and
5 min read
Minimize sum of absolute differences between given Arrays by replacing at most 1 element from first Array Given two arrays a[] and b[] of equal size. The task is to calculate the minimum sum of absolute differences for each index. It is allowed to replace at most 1 element of the first array with any value from the first array itself. Example: Input: a[] = {1, 9, 4, 2}, b[] = {2, 3, 7, 1}Output: 6Explan
9 min read
Minimize length of an array consisting of difference between all possible pairs Given an array arr[] of size N, the task is to find the minimum count of elements required to be inserted into the array such that the absolute difference of all possible pairs exists in the array. Examples: Input: arr[] = { 3, 5 } Output: 3 Explanation: Inserting 2 into the array modifies arr[] to
8 min read
Split array into K subarrays with minimum sum of absolute difference between adjacent elements Given an array, arr[] of size N and an integer K, the task is to split the array into K subarrays minimizing the sum of absolute difference between adjacent elements of each subarray. Examples: Input: arr[] = {1, 3, -2, 5, -1}, K = 2Output: 13Explanation: Split the array into following 2 subarrays:
8 min read