Minimize moves to make Array elements equal by incrementing and decrementing pairs
Last Updated :
21 Mar, 2023
Given an array arr[] of size N, the task is to print the minimum number of moves needed to make all array elements equal by selecting any two distinct indices and then increment the element at the first selected index and decrement the element at the other selected index by 1 in each move. If it is impossible to make all the array elements equal then print “-1“.
Examples:
Input: arr[] = {5, 4, 1, 10}
Output: 5
Explanation:
One of the possible way to perform operation is:
Operation 1: Select the indices 1 and 3 and then increment arr[1] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 1, 9}.
Operation 2: Select the indices 2 and 3 and then increment arr[2] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 2, 8}.
Operation 3: Select the indices 2 and 3 and then increment arr[2] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 3, 7}.
Operation 4: Select the indices 2 and 3 and then increment arr[2] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 4, 6}.
Operation 5: Select the indices 2 and 3 and then increment arr[2] by 1 and decrement arr[3] by 1. Thereafter, the array modifies to {5, 5, 5, 5}.
Therefore, the total number of move needed is 5. Also, it is the minimum possible moves needed.
Input: arr[] = {1, 4}
Output: -1
Approach: The given problem can be solved based on the following observations:
- It can be observed that in one move the sum of the array remains the same therefore, if the sum of the array is not divisible N then it is impossible to make all the array elements equal.
- Otherwise, each array element will be equal to the sum of the array divided by N.
- Therefore, the idea is to use the two pointer technique to find the minimum count of moves needed to make all the array elements equal to the sum/N.
Follow the steps below to solve the problem:
- Initialize a variable, say ans as 0, to store the count of the moves needed.
- Find the sum of the array and store it in a variable say sum.
- Now if the sum is not divisible by N then print “-1“. Otherwise, update the sum as sum =sum/N.
- Sort the array in ascending order.
- Initialize variables, say i as 0 and j as N – 1 to iterate over the array.
- Iterate until i is less than j and perform the following steps:
- If increasing arr[i] to sum is less than decreasing arr[j] to sum then add sum – arr[i] to the ans, and then update arr[i], and arr[j] and then increment i by 1.
- Otherwise, add arr[j] – sum to the ans, and update arr[i] and arr[j] and then decrement j by 1.
- Finally, after completing the above steps, print the value of stored in ans.
Below is the implementation of the above approach:
C++
#include <algorithm>
#include <iostream>
using namespace std;
int find( int arr[], int N)
{
int Sum = 0;
for ( int i = 0; i < N; i++) {
Sum += arr[i];
}
if (Sum % N) {
return -1;
}
Sum /= N;
sort(arr, arr + N);
int ans = 0;
int i = 0, j = N - 1;
while (i < j) {
if (Sum - arr[i] < arr[j] - Sum) {
ans += (Sum - arr[i]);
arr[i] += (Sum - arr[i]);
arr[j] -= (Sum - arr[i]);
i++;
}
else {
ans += (arr[j] - Sum);
arr[i] += (arr[j] - Sum);
arr[j] -= (arr[j] - Sum);
j--;
}
}
return ans;
}
int main()
{
int arr[] = { 5, 4, 1, 10 };
int N = sizeof (arr) / sizeof ( int );
cout << find(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static int find( int arr[], int N)
{
int Sum = 0 ;
for ( int i = 0 ; i < N; i++) {
Sum += arr[i];
}
if (Sum % N > 0 ) {
return - 1 ;
}
Sum /= N;
Arrays.sort(arr);
int ans = 0 ;
int i = 0 , j = N - 1 ;
while (i < j) {
if (Sum - arr[i] < arr[j] - Sum) {
ans += (Sum - arr[i]);
arr[i] += (Sum - arr[i]);
arr[j] -= (Sum - arr[i]);
i++;
} else {
ans += (arr[j] - Sum);
arr[i] += (arr[j] - Sum);
arr[j] -= (arr[j] - Sum);
j--;
}
}
return ans;
}
public static void main(String args[]) {
int arr[] = { 5 , 4 , 1 , 10 };
int N = arr.length;
System.out.println(find(arr, N));
}
}
|
Python3
def find(arr, N):
Sum = sum (arr)
if Sum % N:
return - 1
else :
Sum / / = N
arr.sort()
ans = 0
i = 0
j = N - 1
while i < j:
if Sum - arr[i] < arr[j] - Sum :
ans + = Sum - arr[i]
arr[i] + = Sum - arr[i]
arr[j] - = Sum - arr[i]
i + = 1
else :
ans + = arr[j] - Sum
arr[i] + = arr[j] - Sum
arr[j] - = arr[j] - Sum
j - = 1
return ans
if __name__ = = '__main__' :
arr = [ 5 , 4 , 1 , 10 ]
N = len (arr)
print (find(arr, N))
|
C#
using System;
class GFG{
public static int find( int [] arr, int N)
{
int Sum = 0;
int i = 0;
for (i = 0; i < N; i++)
{
Sum += arr[i];
}
if (Sum % N > 0)
{
return -1;
}
Sum /= N;
Array.Sort(arr);
int ans = 0;
i = 0;
int j = N - 1;
while (i < j)
{
if (Sum - arr[i] < arr[j] - Sum)
{
ans += (Sum - arr[i]);
arr[i] += (Sum - arr[i]);
arr[j] -= (Sum - arr[i]);
i++;
}
else
{
ans += (arr[j] - Sum);
arr[i] += (arr[j] - Sum);
arr[j] -= (arr[j] - Sum);
j--;
}
}
return ans;
}
static public void Main()
{
int [] arr = { 5, 4, 1, 10 };
int N = arr.Length;
Console.WriteLine(find(arr, N));
}
}
|
Javascript
<script>
function find(arr, N) {
let Sum = 0;
for (i = 0; i < arr.length; i++) {
Sum = Sum + arr[i];
}
if (Sum % N)
return -1;
else {
Sum = Math.floor(Sum / N);
arr.sort( function (a, b) { return a - b });
ans = 0
i = 0
j = N - 1
while (i < j) {
if (Sum - arr[i] < arr[j] - Sum) {
ans += Sum - arr[i]
arr[i] += Sum - arr[i]
arr[j] -= Sum - arr[i]
i += 1
}
else {
ans += arr[j] - Sum
arr[i] += arr[j] - Sum
arr[j] -= arr[j] - Sum
j -= 1
}
}
}
return ans;
}
let arr = [5, 4, 1, 10];
let N = arr.length;
document.write(find(arr, N));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Another Approach:
- Compute the sum of all elements in the array and divide it by N to find the target value (the value that all elements in the array should be equal to).
- Iterate over the array, and for each element:
- Compute the difference between the current element and the target value.
- If the difference is odd, return -1 (because we can’t make all elements equal by incrementing and decrementing in pairs).
- Otherwise, add the absolute value of the difference to a variable count (this represents the number of moves needed to make the current element equal to the target value).
- Return the count.
Below is the implementation of above approach:
C++
#include <algorithm>
#include <iostream>
using namespace std;
int find( int arr[], int N) {
int Sum = 0;
for ( int i = 0; i < N; i++) {
Sum += arr[i];
}
if (Sum % N) {
return -1;
}
Sum /= N;
sort(arr, arr + N);
int ans = 0, i = 0;
while (i < N) {
int j = i + 1;
while (j < N && arr[j] <= Sum) {
j++;
}
if (j == N) {
break ;
}
ans += j - i;
arr[i] += j - i;
arr[j] -= j - i;
i++;
}
return ans;
}
int main() {
int arr[] = { 5, 4, 1, 10 };
int N = sizeof (arr) / sizeof ( int );
cout << find(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static int find( int arr[], int N) {
int Sum = 0 ;
for ( int i = 0 ; i < N; i++) {
Sum += arr[i];
}
if (Sum % N != 0 ) {
return - 1 ;
}
int targetSum = Sum / N;
Arrays.sort(arr);
int ans = 0 , i = 0 ;
while (i < N) {
int j = i + 1 ;
while (j < N && arr[j] <= targetSum) {
j++;
}
if (j == N) {
break ;
}
ans += j - i;
arr[i] += j - i;
arr[j] -= j - i;
i++;
}
return ans;
}
public static void main(String[] args) {
int arr[] = { 5 , 4 , 1 , 10 };
int N = arr.length;
System.out.println(find(arr, N));
}
}
|
Python3
def find(arr, N):
Sum = sum (arr)
if Sum % N:
return - 1
Sum / / = N
arr.sort()
ans, i = 0 , 0
while i < N:
j = i + 1
while j < N and arr[j] < = Sum :
j + = 1
if j = = N:
break
ans + = j - i
arr[i:i + j - i] = [arr[i] + j - i] * (j - i)
arr[j] - = j - i
i + = 1
return ans
arr = [ 5 , 4 , 1 , 10 ]
N = len (arr)
print (find(arr, N))
|
Javascript
function find(arr, N) {
let Sum = 0;
for (let i = 0; i < N; i++) {
Sum += arr[i];
}
if (Sum % N) {
return -1;
}
Sum /= N;
arr.sort((a, b) => a - b);
let ans = 0, i = 0;
while (i < N) {
let j = i + 1;
while (j < N && arr[j] <= Sum) {
j++;
}
if (j == N) {
break ;
}
ans += j - i;
arr[i] += j - i;
arr[j] -= j - i;
i++;
}
return ans;
}
let arr = [5, 4, 1, 10];
let N = arr.length;
console.log(find(arr, N));
|
C#
using System;
using System.Collections.Generic;
class Gfg{
static int find( int [] arr, int N) {
int Sum = 0, i=0;
for ( i = 0; i < N; i++) {
Sum += arr[i];
}
if (Sum % N !=0) {
return -1;
}
Sum /= N;
Array.Sort(arr);
int ans = 0;
i = 0;
while (i < N) {
int j = i + 1;
while (j < N && arr[j] <= Sum) {
j++;
}
if (j == N) {
break ;
}
ans += j - i;
arr[i] += j - i;
arr[j] -= j - i;
i++;
}
return ans;
}
public static void Main(String[] args)
{
int [] arr = { 5, 4, 1, 10 };
int N = arr.Length;
Console.Write(find(arr, N));
}
}
|
Time Complexity: O(N), where N is the size of the input array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...