Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2
Last Updated :
23 Jul, 2021
Given an array arr[] of size N, the task is to print the minimum number of moves required to make all array elements equal by selecting any pair of distinct indices and then increment the element at the first index and decrement the element at the other index by 1 each time. If it is impossible to make all array elements equal, then print “-1“.
Examples:
Input: arr[] = {5, 4, 1, 10}
Output: 5
Explanation:
One of the possible ways to perform operations 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 moves needed is 5. Also, it is the minimum possible moves needed.
Input: arr[] = {1, 4}
Output: -1
Naive Approach: Refer to the previous post for the simplest approach to solve the problem.
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
- It can be observed that in each operation, 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, say k.
- Therefore, the idea is to iterate over the array and find the absolute difference between the current element and k and add to ans.
- Since in each operation, both increments and decrements are performed together, ans / 2 is the number of operations required.
Follow the steps below to solve the problem:
- Initialize a variable, say ans as 0, to store the count of the operations required.
- 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, store k = sum / N.
- Initialize variables, say i = 0, and traverse the array.
- Iterate while i is less than N and add the absolute difference between the current element and k to ans:
- Finally, after completing the above steps, print ans / 2.
Below is the implementation of the above approach:
C++
// cpp program for the above approach
// Function to find the minimum number
// of increment and decrement of pairs
// required to make all array elements equal
#include<bits/stdc++.h>
using namespace std;
int find(vector<int>arr, int N)
{
// Stores the sum of the array
int Sum = 0;
for(auto i:arr)
Sum += i;
// If sum is not divisible by N
if (Sum % N)
return -1;
// Update sum
int k = Sum / N;
int ans = 0;
// Store the minimum
// number of operations
int i = 0;
// Iterate while i
// is less than N
while (i < N){
// Add absolute difference
// of current element with
// k to ans
ans = ans + abs(k-arr[i]);
// Increase i bye 1
i += 1;
}
// Return the value in ans//2
return ans /2;
}
// Driver Code
int main()
{
// Given Input
vector<int>arr = {5, 4, 1, 10};
int N = arr.size();
// Function Call
cout<<(find(arr, N));
}
// This code is contributed by amreshkumar3.
Java
// Java program for the above approach
import java.util.*;
class GFG
{
static int find(ArrayList<Integer>arr, int N)
{
// Stores the sum of the array
int Sum = 0;
for(int item : arr)
Sum += item;
// If sum is not divisible by N
if (Sum % N==1)
return -1;
// Update sum
int k = Sum / N;
int ans = 0;
// Store the minimum
// number of operations
int i = 0;
// Iterate while i
// is less than N
while (i < N){
// Add absolute difference
// of current element with
// k to ans
ans = ans + Math.abs(k-arr.get(i));
// Increase i bye 1
i += 1;
}
// Return the value in ans//2
return ans /2;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
ArrayList<Integer>arr = new ArrayList<>();
arr.add(5);
arr.add(4);
arr.add(1);
arr.add(10);
int N = arr.size();
// Function Call
System.out.println(find(arr, N));
}
}
// This code is contributed by sanjoy_62.
Python3
# Python program for the above approach
# Function to find the minimum number
# of increment and decrement of pairs
# required to make all array elements equal
def find(arr, N):
# Stores the sum of the array
Sum = sum(arr)
# If sum is not divisible by N
if Sum % N:
return -1
else:
# Update sum
k = Sum // N
# Store the minimum
# number of operations
ans = 0
i = 0
# Iterate while i
# is less than N
while i < N:
# Add absolute difference
# of current element with
# k to ans
ans = ans + abs(k-arr[i])
# Increase i bye 1
i += 1
# Return the value in ans//2
return ans // 2
# Driver Code
if __name__ == '__main__':
# Given Input
arr = [5, 4, 1, 10]
N = len(arr)
# Function Call
print(find(arr, N))
C#
// C# program for the above approach
// Function to find the minimum number
// of increment and decrement of pairs
// required to make all array elements equal
using System;
using System.Collections.Generic;
class GFG{
static int find(List<int>arr, int N)
{
// Stores the sum of the array
int Sum = 0;
foreach(int item in arr)
Sum += item;
// If sum is not divisible by N
if (Sum % N==1)
return -1;
// Update sum
int k = Sum / N;
int ans = 0;
// Store the minimum
// number of operations
int i = 0;
// Iterate while i
// is less than N
while (i < N){
// Add absolute difference
// of current element with
// k to ans
ans = ans + Math.Abs(k-arr[i]);
// Increase i bye 1
i += 1;
}
// Return the value in ans//2
return ans /2;
}
// Driver Code
public static void Main()
{
// Given Input
List<int>arr = new List<int>(){5, 4, 1, 10};
int N = arr.Count;
// Function Call
Console.Write(find(arr, N));
}
}
// This code is contributed by bgangwar59.
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the minimum number
// of increment and decrement of pairs
// required to make all array elements equal
function find(arr, N)
{
// Stores the sum of the array
let Sum = 0;
let i = 0;
let ans = 0;
for(i = 0; i < N; i++)
{
Sum += arr[i];
}
// If sum is not divisible by N
if (Sum % N)
{
return -1;
}
else
{
// Update sum
k = Math.floor(Sum / N);
// Store the minimum
// number of operations
ans = 0;
i = 0;
// Iterate while i
// is less than N
while (i < N)
{
// Add absolute difference
// of current element with
// k to ans
ans = ans + Math.abs(k - arr[i]);
// Increase i bye 1
i += 1;
}
}
// Return the value in ans//2
return Math.floor(ans / 2);
}
// Driver Code
// Given Input
let arr = [ 5, 4, 1, 10 ]
let N = arr.length;
// Function Call
document.write(find(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize moves to make Array elements equal by incrementing and decrementing pairs 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
14 min read
Minimum increment and decrement by K of each pair elements required to make all array elements equal Given an array arr[], the task is to check if it is possible to make all array elements equal by repeatedly choosing a triplet (i, j, k), where i and j are different, and subtract k from arr[i] and add k to arr[j]. Examples: Input: arr[] = {1, 5, 6, 4}Output: YesExplanation:Operations performed: Cho
10 min read
Minimize increment or increment and decrement of Pair to make all Array elements equal Given an array arr[] of size N, the task is to minimize the number of steps to make all the array elements equal by performing the following operations: Choose an element of the array and increase it by 1.Select two elements simultaneously (arr[i], arr[j]) increase arr[i] by 1 and decrease arr[j] by
8 min read
Minimize cost of insertions and deletions required to make all array elements equal Given a sorted array arr[] of size N (1 ? N ? 105) and two integers A and B, the task is to calculate the minimum cost required to make all array elements equal by increments or decrements. The cost of each increment and decrement are A and B respectively. Examples: Input: arr[] = { 2, 5, 6, 9, 10,
11 min read
Minimum increments or decrements by 1 required to make all array elements in AP Given an array arr[] consisting of N integers, the task is to find the minimum number of increment/decrement by 1 required to be performed on array elements to make all the elements of the given array arr[] in AP. If it is not possible to make the array in AP, then print "-1". Examples: Input: arr[]
11 min read
Minimize increments required to make differences between all pairs of array elements even Given an array, arr[] consisting of N integers, the task is to minimize the number of increments of array elements required to make all differences pairs of array elements even. Examples: Input: arr[] = {4, 1, 2}Output: 1Explanation: Operation 1: Increment arr[1] by 1. The array arr[] modifies to {4
5 min read