Minimum decrement operations to make Array elements equal by only decreasing K each time
Last Updated :
27 Dec, 2022
Given an array arr[] of size N consisting of positive integers and an integer K, the task is to find the minimum number of steps required to make all the elements of the array equal such that at each step, one value from the array can be selected and decremented by K. Print -1 if the array can’t be made equal.
Examples:
Input: arr[] = {12, 9, 15}, K = 3
Output: 3
Explanation:
Initially: {12, 9, 15}
After decreasing K from 15 at position 3: [12, 9, 12]
After decreasing K from 12 at position 1: [9, 9, 12]
After decreasing K from 12 at position 3: [9, 9, 9]
Input: arr[] = {10, 9}, K = 2
Output: -1
Explanation:
It is impossible to equalize all elements
Approach: The idea is to keep the minimum valued elements unaffected and count the number of decrement operations taken by the other elements to reach this minimum value. The following steps can be followed to compute the result:
- Find the minimum element minx in the array.
- Once the minimum value is found, a variable decrements is maintained and initialized to 0.
- Then a loop is run over all elements, adding (arr[i]-minx)/K to the decrements variable.
- If any arr[i] is encountered such that arr[i]-minx is not divisible by K, then return -1 as it can’t be decreased to the minimum value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
lli solve(lli arr[], lli n, lli k)
{
lli i, minx = INT_MAX;
for (i = 0; i < n; i++) {
minx = min(minx, arr[i]);
}
lli decrements = 0;
for (i = 0; i < n; i++) {
if ((arr[i] - minx) % k != 0) {
return -1;
}
else {
decrements += ((arr[i] - minx) / k);
}
}
return decrements;
}
int main()
{
lli n, k;
n = 3;
k = 3;
lli arr[n] = { 12, 9, 15 };
cout << solve(arr, n, k);
}
|
Java
class GFG
{
static int INT_MAX = Integer.MAX_VALUE ;
static int solve( int arr[], int n, int k)
{
int minx = INT_MAX;
int i;
for (i = 0 ; i < n; i++)
{
minx = Math.min(minx, arr[i]);
}
int decrements = 0 ;
for (i = 0 ; i < n; i++)
{
if ((arr[i] - minx) % k != 0 )
{
return - 1 ;
}
else
{
decrements += ((arr[i] - minx) / k);
}
}
return decrements;
}
public static void main (String[] args)
{
int n, k;
n = 3 ;
k = 3 ;
int arr[] = { 12 , 9 , 15 };
System.out.println(solve(arr, n, k));
}
}
|
Python3
import sys
def solve(arr, n, k) :
minx = sys.maxsize;
for i in range (n) :
minx = min (minx, arr[i]);
decrements = 0 ;
for i in range (n) :
if ((arr[i] - minx) % k ! = 0 ) :
return - 1 ;
else :
decrements + = ((arr[i] - minx) / / k);
return decrements;
if __name__ = = "__main__" :
n = 3 ;
k = 3 ;
arr = [ 12 , 9 , 15 ];
print (solve(arr, n, k));
|
C#
using System;
class GFG
{
static int INT_MAX = int .MaxValue ;
static int solve( int []arr, int n, int k)
{
int minx = INT_MAX;
int i;
for (i = 0; i < n; i++)
{
minx = Math.Min(minx, arr[i]);
}
int decrements = 0;
for (i = 0; i < n; i++)
{
if ((arr[i] - minx) % k != 0)
{
return -1;
}
else
{
decrements += ((arr[i] - minx) / k);
}
}
return decrements;
}
public static void Main()
{
int n, k;
n = 3;
k = 3;
int []arr = { 12, 9, 15 };
Console.WriteLine(solve(arr, n, k));
}
}
|
Javascript
<script>
var INT_MAX = Number.MAX_VALUE;
function solve(arr , n , k) {
var minx = INT_MAX;
var i;
for (i = 0; i < n; i++) {
minx = Math.min(minx, arr[i]);
}
var decrements = 0;
for (i = 0; i < n; i++) {
if ((arr[i] - minx) % k != 0)
{
return -1;
}
else
{
decrements += ((arr[i] - minx) / k);
}
}
return decrements;
}
var n, k;
n = 3;
k = 3;
var arr = [ 12, 9, 15 ];
document.write(solve(arr, n, k));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...