Minimum decrement operations to make Array elements equal by only decreasing K each time
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++
// C++ implementation of the above approach #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; // Finding the minimum element for (i = 0; i < n; i++) { minx = min(minx, arr[i]); } lli decrements = 0; // Loop over all the elements // and find the difference for (i = 0; i < n; i++) { if ((arr[i] - minx) % k != 0) { return -1; } else { decrements += ((arr[i] - minx) / k); } } // Solution found and returned return decrements; } // Driver code int main() { lli n, k; n = 3; k = 3; lli arr[n] = { 12, 9, 15 }; cout << solve(arr, n, k); } |
Java
// Java implementation of the above approach class GFG { static int INT_MAX = Integer.MAX_VALUE ; static int solve( int arr[], int n, int k) { int minx = INT_MAX; int i; // Finding the minimum element for (i = 0 ; i < n; i++) { minx = Math.min(minx, arr[i]); } int decrements = 0 ; // Loop over all the elements // and find the difference for (i = 0 ; i < n; i++) { if ((arr[i] - minx) % k != 0 ) { return - 1 ; } else { decrements += ((arr[i] - minx) / k); } } // Solution found and returned return decrements; } // Driver code 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)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the above approach import sys def solve(arr, n, k) : minx = sys.maxsize; # Finding the minimum element for i in range (n) : minx = min (minx, arr[i]); decrements = 0 ; # Loop over all the elements # and find the difference for i in range (n) : if ((arr[i] - minx) % k ! = 0 ) : return - 1 ; else : decrements + = ((arr[i] - minx) / / k); # Solution found and returned return decrements; # Driver code if __name__ = = "__main__" : n = 3 ; k = 3 ; arr = [ 12 , 9 , 15 ]; print (solve(arr, n, k)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the above approach 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; // Finding the minimum element for (i = 0; i < n; i++) { minx = Math.Min(minx, arr[i]); } int decrements = 0; // Loop over all the elements // and find the difference for (i = 0; i < n; i++) { if ((arr[i] - minx) % k != 0) { return -1; } else { decrements += ((arr[i] - minx) / k); } } // Solution found and returned return decrements; } // Driver code public static void Main() { int n, k; n = 3; k = 3; int []arr = { 12, 9, 15 }; Console.WriteLine(solve(arr, n, k)); } } // This code is contributed by AnkitRai01 |
Javascript
<script> // javascript implementation of the above approach var INT_MAX = Number.MAX_VALUE; function solve(arr , n , k) { var minx = INT_MAX; var i; // Finding the minimum element for (i = 0; i < n; i++) { minx = Math.min(minx, arr[i]); } var decrements = 0; // Loop over all the elements // and find the difference for (i = 0; i < n; i++) { if ((arr[i] - minx) % k != 0) { return -1; } else { decrements += ((arr[i] - minx) / k); } } // Solution found and returned return decrements; } // Driver code var n, k; n = 3; k = 3; var arr = [ 12, 9, 15 ]; document.write(solve(arr, n, k)); // This code is contributed by Rajput-Ji. </script> |
3
Time complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.