Minimum increments or decrements by D required to make all array elements equal
Last Updated :
04 May, 2021
Given an array arr[] of size N and an integer D, the task is to make all array elements equal by incrementing or decrementing minimum number of array elements by D. If it is not possible to make all the array elements equal, then print -1.
Examples :
Input: N = 4, d = 2, arr[ ] = {2, 4, 6, 8}
Output: 4
Explanation:
Incrementing arr[0] by D(=2) modifies arr[] to { 4, 4, 6, 8 }.
Decrementing arr[2] by D(=2) modifies arr[] to { 4, 4, 4, 8 }.
Decrementing arr[3] by D(=2) modifies arr[] to { 4, 4, 4, 6 }.
Decrementing arr[3] by D(=2) modifies arr[] to { 4, 4, 4, 4 }.
Input: N = 4, K = 3, arr[ ] = {2, 4, 5, 6}
Output: -1
Approach: The idea is to use Greedy Approach to solve this problem. Below are the steps:
- Sort the given array in ascending order.
- Observe that only those array elements can be made equal, whose difference with adjacent indexed element is completely divisible by D, i.e. (a[i + 1] – a[i]) % D == 0.
- If any of the differences is not divisible by D, then print -1 as all the array elements cannot be made equal.
- Now, use a greedy approach here. For minimum operation, consider the mid element and try to change all other elements to mid. Find the number of operations required for the transformation of one number i.e. abs(mid – a[i]) / D.
- In the end, print the minimum number of operations required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void numOperation( int arr[], int N, int D)
{
sort(arr, arr + N);
for ( int i = 0; i < N - 1; i++) {
if ((arr[i + 1] - arr[i]) % D != 0) {
cout << "-1" ;
return ;
}
}
int count = 0;
int mid = arr[N / 2];
for ( int i = 0; i < N; i++) {
count += abs (mid - arr[i]) / D;
}
cout << count;
}
int main()
{
int N = 4, D = 2;
int arr[] = { 2, 4, 6, 8 };
numOperation(arr, N, D);
}
|
Java
import java.util.*;
class GFG{
static void numOperation( int arr[], int N, int D)
{
Arrays.sort(arr);
for ( int i = 0 ; i < N - 1 ; i++) {
if ((arr[i + 1 ] - arr[i]) % D != 0 ) {
System.out.println( "-1" );
return ;
}
}
int count = 0 ;
int mid = arr[N / 2 ];
for ( int i = 0 ; i < N; i++) {
count += Math.abs(mid - arr[i]) / D;
}
System.out.println(count);
}
public static void main(String[] args)
{
int N = 4 , D = 2 ;
int arr[] = { 2 , 4 , 6 , 8 };
numOperation(arr, N, D);
}
}
|
Python3
def numOperation(arr, N, D):
arr.sort()
for i in range (N - 1 ):
if ((arr[i + 1 ] - arr[i]) % D ! = 0 ):
print ( "-1" )
return
count = 0
mid = arr[N / / 2 ]
for i in range (N):
count + = abs (mid - arr[i]) / / D
print (count)
if __name__ = = "__main__" :
N = 4
D = 2
arr = [ 2 , 4 , 6 , 8 ]
numOperation(arr, N, D)
|
C#
using System;
class GFG
{
static void numOperation( int [] arr, int N, int D)
{
Array.Sort(arr);
for ( int i = 0; i < N - 1; i++)
{
if ((arr[i + 1] - arr[i]) % D != 0)
{
Console.WriteLine( "-1" );
return ;
}
}
int count = 0;
int mid = arr[N / 2];
for ( int i = 0; i < N; i++)
{
count += Math.Abs(mid - arr[i]) / D;
}
Console.WriteLine(count);
}
static public void Main()
{
int N = 4, D = 2;
int [] arr = new int [] { 2, 4, 6, 8 };
numOperation(arr, N, D);
}
}
|
Javascript
<script>
function numOperation(arr, N, D)
{
arr.sort();
for (let i = 0; i < N - 1; i++)
{
if ((arr[i + 1] - arr[i]) % D != 0)
{
document.write( "-1" );
return ;
}
}
let count = 0;
let mid = arr[N / 2];
for (let i = 0; i < N; i++)
{
count += Math.abs(mid - arr[i]) / D;
}
document.write(count);
}
let N = 4, D = 2;
let arr = [ 2, 4, 6, 8 ];
numOperation(arr, N, D);
</script>
|
Time Complexity: O(N * Log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...