Arrays

# Minimum sum of all differences between unique pairs in the Array

Given an array arr[] consisting of N integers, the task is to find the minimum sum of all absolute differences between unique pairs of elements in the array after updating the array arr[]. To update the array, any two elements from the array can be chosen in any order. 1 is subtracted from the first element and added to the second element. This step can be repeated any number of times.Examples:Input: arr[]={1, 2, 3}Output: 0Explanation: Choosing 1 and 3 for updating the array, 3-1=2 and 1+1=2. So the updated array becomes arr[]={2, 2, 2}. Minimum possible sum of the absolute difference of pairs is 0+0+0=0. Input: arr[]={0, 1, 1, 0}Output: 4Explanation: Choosing any two elements for updation will lead to increase in total sum of absolute differences. So the sum is 1+1+1+1+0+0=4.Approach: The given problem can be solved based on the following observations:To minimize the sum of absolute differences between all the unique pairs, the elements should be as closest as possible.To make that, find the sum S and divide sum S by N i.e, val = S/N.If val is an integer, then the problem becomes very simple as following the operations all the integers can be converted to X.Else, some elements say X will be the integer just less than val, i.e, floor(val) and others will be ceil(val).For finding the value of X, use the fact that even after updating the sum of the array will be same. So, use the following equation to find the value of X.  x*(b-1) + N*b – x*b = S=>  x*b – x + N*b – x*b = S=>  N*b – x = S=>  x = N*b – SN = 10arr[] = {8, 3, 6, 11, 5, 2, 1, 7, 10, 4}Sum of all elements of the array, S=57So, S/N = 5.7 means if all the elements are converted to 5.7, then the sum of all absolute differences b/w all unique pairs of elements will be minimized i.e, 0.But converting all elements to 5.7 using the mentioned operation isn’t possible.So, some elements will be 5 and the others will be 6. Now, the question is how many elements will be 5. Let’s say x, then (N-x) will be 6 and in all this process sum will be always be conserved.=> x*5 + (N-x)*6 = 57=> -x = 57 – 60 (Putting the value of N as 10 and solving the equation)-x = -3 => x=3So, the converted array will be {5, 5, 5, 6, 6, 6, 6, 6, 6, 6}Now the pairs which will give a difference like 1 are x*(N-x).So the total sum of differences will also be x*(N-x).Follow the steps below to solve the problem:Initialize the variable sum as 0 to store the sum of elements in the array arr[].Iterate over the range [0, N] using the variable i and perform the following steps:Add the value of arr[i] in the variable sum.Initialize the variable temp as (float)S/N.Set the value of a as the floor value of temp.Set the value of b as the ceil value of temp.Set the value of x as b*N-sum to store the position where partition will happen.Print the value of x*(N-x) as the result.Below is the implementation of the above approach:C++#include using namespace std;  void solve(int N, int arr[]){    int a, b, S = 0, x;    for (int i = 0; i < N; i++) {                S = S + arr[i];    }                  float temp = (float)S / N;          a = floor(temp);        b = ceil(temp);        x = b * N - S;        cout 