Given an array arr[] and an integer k. You can perform an operation in which you can increment any of the number in the array by k. Find the minimum number of operations needed to make all the elements of array equal.
Note: If it is not possible to make all elements of array equal return -1.
Examples:Â
Input: arr[] = [4, 4, 4, 2], k = 2
Output: 1
Explanation: We can increment the element at last index of the array by 2 to make all the elements equal to 4.
Input: arr[] = [4, 2, 6, 8], k = 3
Output: -1
Explanation: It can be proven that these elements can't be made equal by applying any number of operations.Input: arr[] = [4, 7, 19, 16], k = 3
Output: 10
Explanation: The maximum element in the array is 19. Since we can only increment elements, all elements must be made equal to 19.
4 -> 19: Difference = 15, Operations = 15 / 3 = 5
7 -> 19: Difference = 12, Operations = 12 / 3 = 4
19 -> 19: Difference = 0, Operations = 0
16 -> 19: Difference = 3, Operations = 3 / 3 = 1
Total operations = 5 + 4 + 0 + 1 = 10.
Table of Content
[Naive Approach] Try Every Element as the Target - O(n^2) Time and O(1) Space
The idea is to assume each array element as the final value and check whether all other elements can be incremented to match it.
Working of Approach:
- Initialize the answer as a large value.
- Traverse the array and consider each element as the target value.
- For every target, traverse the array again.
- If an element is greater than the target or its difference with the target is not divisible by k, mark the target as invalid.
- Otherwise, add the required operations for the current element.
- Update the minimum operations if the current target is valid.
- Return the minimum operations if a valid target exists; otherwise, return -1.
#include <bits/stdc++.h>
using namespace std;
int minOps(vector<int> &arr, int k) {
int ans = INT_MAX;
for (int target : arr) {
int operations = 0;
bool valid = true;
for (int x : arr) {
if (x > target || (target - x) % k != 0) {
valid = false;
break;
}
operations += (target - x) / k;
}
if (valid) {
ans = min(ans, operations);
}
}
return ans == INT_MAX ? -1 : ans;
}
int main() {
vector<int> arr = {4, 4, 4, 2};
int k = 2;
cout << minOps(arr, k);
return 0;
}
public class GFG {
static int minOps(int[] arr, int k) {
int ans = Integer.MAX_VALUE;
for (int target : arr) {
int operations = 0;
boolean valid = true;
for (int x : arr) {
if (x > target || (target - x) % k != 0) {
valid = false;
break;
}
operations += (target - x) / k;
}
if (valid) {
ans = Math.min(ans, operations);
}
}
return ans == Integer.MAX_VALUE ? -1 : ans;
}
public static void main(String[] args) {
int[] arr = {4, 4, 4, 2};
int k = 2;
System.out.println(minOps(arr, k));
}
}
def minOps(arr, k):
ans = float("inf")
for target in arr:
operations = 0
valid = True
for x in arr:
if x > target or (target - x) % k != 0:
valid = False
break
operations += (target - x) // k
if valid:
ans = min(ans, operations)
return -1 if ans == float("inf") else ans
if __name__ == "__main__":
arr = [4, 4, 4, 2]
k = 2
print(minOps(arr, k))
using System;
class GFG {
static int minOps(int[] arr, int k) {
int ans = int.MaxValue;
foreach (int target in arr) {
int operations = 0;
bool valid = true;
foreach (int x in arr) {
if (x > target || (target - x) % k != 0) {
valid = false;
break;
}
operations += (target - x) / k;
}
if (valid) {
ans = Math.Min(ans, operations);
}
}
return ans == int.MaxValue ? -1 : ans;
}
static void Main() {
int[] arr = {4, 4, 4, 2};
int k = 2;
Console.WriteLine(minOps(arr, k));
}
}
function minOps(arr, k) {
let ans = Number.MAX_SAFE_INTEGER;
for (const target of arr) {
let operations = 0;
let valid = true;
for (const x of arr) {
if (x > target || (target - x) % k !== 0) {
valid = false;
break;
}
operations += Math.floor((target - x) / k);
}
if (valid) {
ans = Math.min(ans, operations);
}
}
return ans === Number.MAX_SAFE_INTEGER ? -1 : ans;
}
// Driver Code
const arr = [4, 4, 4, 2];
const k = 2;
console.log(minOps(arr, k));
Output
1
[Expected Approach] Make Every Element Equal to the Maximum - O(n) Time and O(1) Space
The idea is to make every element equal to the maximum element of the array. Since we are only allowed to increment elements, no element can be reduced, so the final value cannot be smaller than the current maximum.
- Find the maximum element in the array.
- Traverse the array.
- For each element, check whether the difference between the maximum element and the current element is divisible by k.
- If any difference is not divisible by k, return -1.
- Otherwise, add the required operations for the current element.
- Return the total number of operations.
#include <bits/stdc++.h>
using namespace std;
int minOps(vector<int> &arr, int k) {
int maxVal = *max_element(arr.begin(), arr.end());
int operations = 0;
for (int x : arr) {
if ((maxVal - x) % k != 0) {
return -1;
}
operations += (maxVal - x) / k;
}
return operations;
}
int main() {
vector<int> arr = {4, 4, 4, 2};
int k = 2;
cout << minOps(arr, k);
return 0;
}
public class GFG {
static int minOps(int[] arr, int k) {
int maxVal = Integer.MIN_VALUE;
for (int x : arr) {
maxVal = Math.max(maxVal, x);
}
int operations = 0;
for (int x : arr) {
if ((maxVal - x) % k != 0) {
return -1;
}
operations += (maxVal - x) / k;
}
return operations;
}
public static void main(String[] args) {
int[] arr = {4, 4, 4, 2};
int k = 2;
System.out.println(minOps(arr, k));
}
}
def minOps(arr, k):
maxVal = max(arr)
operations = 0
for x in arr:
if (maxVal - x) % k != 0:
return -1
operations += (maxVal - x) // k
return operations
if __name__ == "__main__":
arr = [4, 4, 4, 2]
k = 2
print(minOps(arr, k))
using System;
using System.Linq;
class GFG {
static int minOps(int[] arr, int k) {
int maxVal = arr.Max();
int operations = 0;
foreach (int x in arr) {
if ((maxVal - x) % k != 0) {
return -1;
}
operations += (maxVal - x) / k;
}
return operations;
}
static void Main() {
int[] arr = {4, 4, 4, 2};
int k = 2;
Console.WriteLine(minOps(arr, k));
}
}
function minOps(arr, k) {
const maxVal = Math.max(...arr);
let operations = 0;
for (const x of arr) {
if ((maxVal - x) % k !== 0) {
return -1;
}
operations += Math.floor((maxVal - x) / k);
}
return operations;
}
// Driver Code
const arr = [4, 4, 4, 2];
const k = 2;
console.log(minOps(arr, k));
Output
1