# Minimum increment by k operations to make all elements equal

You are given an array of n-elements, you have to find the number of operations needed to make all elements of array equal. Where a single operation can increment an element by k. If it is not possible to make all elements equal print -1.

Example :

```Input : arr[] = {4, 7, 19, 16},  k = 3
Output : 10

Input : arr[] = {4, 4, 4, 4}, k = 3
Output : 0

Input : arr[] = {4, 2, 6, 8}, k = 3
Output : -1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

To solve this question we require to check whether all elements can became equal or not and that too only by incrementing k from elements value. For this we have to check that the difference of any two elements should always be divisible by k. If it is so, then all elements can become equal otherwise they can not became equal in any case by incrementing k from them. Also, the number of operations required can be calculated by finding value of (max – Ai)/k for all elements. where max is maximum element of array.

Algorithm :

```// iterate for all elements
for (int i=0; i<n; i++)
{
// check if element can make equal to max
//  or not if not then return -1
if ((max - arr[i]) % k != 0 )
return -1;

// else update res for required operations
else
res += (max - arr[i]) / k ;
}

return res;
```

## C++

 `// Program to make all array equal ` `#include ` `using` `namespace` `std; ` ` `  `// function for calculating min operations ` `int` `minOps(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``// max elements of array ` `    ``int` `max = *max_element(arr, arr + n); ` `    ``int` `res = 0; ` ` `  `    ``// iterate for all elements ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// check if element can make equal to ` `        ``// max or not if not then return -1 ` `        ``if` `((max - arr[i]) % k != 0) ` `            ``return` `-1; ` ` `  `        ``// else update res for required operations ` `        ``else` `            ``res += (max - arr[i]) / k; ` `    ``} ` ` `  `    ``// return result ` `    ``return` `res; ` `} ` ` `  `// driver program ` `int` `main() ` `{ ` `    ``int` `arr[] = { 21, 33, 9, 45, 63 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `k = 6; ` `    ``cout << minOps(arr, n, k); ` `    ``return` `0; ` `} `

## Java

 `// Program to make all array equal ` `import` `java.io.*; ` `import` `java.util.Arrays; ` ` `  `class` `GFG { ` `    ``// function for calculating min operations ` `    ``static` `int` `minOps(``int` `arr[], ``int` `n, ``int` `k) ` `    ``{ ` `        ``// max elements of array ` `        ``Arrays.sort(arr); ` `        ``int` `max = arr[arr.length - ``1``]; ` `        ``int` `res = ``0``; ` ` `  `        ``// iterate for all elements ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `            ``// check if element can make equal to ` `            ``// max or not if not then return -1 ` `            ``if` `((max - arr[i]) % k != ``0``) ` `                ``return` `-``1``; ` ` `  `            ``// else update res for required operations ` `            ``else` `                ``res += (max - arr[i]) / k; ` `        ``} ` ` `  `        ``// return result ` `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver program ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = { ``21``, ``33``, ``9``, ``45``, ``63` `}; ` `        ``int` `n = arr.length; ` `        ``int` `k = ``6``; ` `        ``System.out.println(minOps(arr, n, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## Python3

 `# Python3 Program to make all array equal ` ` `  `# function for calculating min operations ` `def` `minOps(arr, n, k): ` ` `  `    ``# max elements of array ` `    ``max1 ``=` `max``(arr) ` `    ``res ``=` `0` ` `  `    ``# iterate for all elements ` `    ``for` `i ``in` `range``(``0``, n):  ` ` `  `        ``# check if element can make equal to ` `        ``# max or not if not then return -1 ` `        ``if` `((max1 ``-` `arr[i]) ``%` `k !``=` `0``): ` `            ``return` `-``1` ` `  `        ``# else update res fo ` `        ``# required operations ` `        ``else``: ` `            ``res ``+``=` `(max1 ``-` `arr[i]) ``/` `k ` `     `  `    ``# return result ` `    ``return` `int``(res) ` ` `  `# driver program ` `arr ``=` `[``21``, ``33``, ``9``, ``45``, ``63``]  ` `n ``=` `len``(arr) ` `k ``=` `6` `print``(minOps(arr, n, k)) ` ` `  `# This code is contributed by  ` `# Smitha Dinesh Semwal `

## C#

 `// C# program Minimum increment by ` `// k operations to make all elements equal. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// function for calculating min operations ` `    ``static` `int` `minOps(``int``[] arr, ``int` `n, ``int` `k) ` `    ``{ ` `         `  `        ``// max elements of array ` `        ``Array.Sort(arr); ` `        ``int` `max = arr[arr.Length - 1]; ` `        ``int` `res = 0; ` ` `  `        ``// iterate for all elements ` `        ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `            ``// check if element can make ` `            ``// equal to max or not if not ` `            ``// then return -1 ` `            ``if` `((max - arr[i]) % k != 0) ` `                ``return` `-1; ` ` `  `            ``// else update res for required ` `            ``// operations ` `            ``else` `                ``res += (max - arr[i]) / k; ` `        ``} ` ` `  `        ``// return result ` `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { 21, 33, 9, 45, 63 }; ` `        ``int` `n = arr.Length; ` `        ``int` `k = 6; ` ` `  `        ``Console.Write(minOps(arr, n, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal. `

## PHP

 ` `

Output :

```24
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.