Given an array and k, we need to find the minimum operations needed to make GCD of the array equal or multiple of k. Here an operation mean either increment or decrement an array element by 1.

Examples:

Input :a = { 4, 5, 6 }, k = 5

Output :2

Explanation :We can increase 4 by 1 so that it becomes 5 and decrease 6 by 1 so that it becomes 5. Hence minimum operation will be 2.

Input :a = { 4, 9, 6 }, k = 5

Output :3

Explanation :Just like previous example we can increase and decrease 4 and 6 by 1 and increase 9 by 1 so that it becomes 10. Now each element have GCD 5. Hence minimum operation will be 3.

Here we have to make the gcd of the array equal or multiple to k, which means there will be cases in which all the elements are near are greater than k or is near to some of its multiple. So, to solve this we just have to make each array value equal to or multiple to K. By doing this we will achieve our solution, as if every element is multiple of k then it’s GCD will be atleast K. Now our next target is to convert the array elements in the minimum operation i.e. minimum number of increment and decrement. This minimum value of increment or decrement can be known only by taking the remainder of each number from K i.e. *either we have to take the remainder value or (k-remainder) value, whichever is minimum among them.*

Below is C++ implementation of this approach

[sourcecode language=”CPP”]

// CPP program to make GCD of array a mutiple

// of k.

#include <bits/stdc++.h>

using namespace std;

int MinOperation(int a[], int n, int k)

{

int result = 0;

for (int i = 0; i < n; ++i) {

// If array value is not 1

// and it is greater than k

// then we can increase the

// or decrease the remainder

// obtained by dividing k

// from the ith value of array

// so that we get the number

// which is either closer to k

// or its multiple

if (a[i] != 1 && a[i] > k) {

result = result + min(a[i] % k, k – a[i] % k);

}

else {

// Else we only have one choice

// which is to increment the value

// to make equal to k

result = result + k – a[i];

}

}

return result;

}

// Driver code

int main()

{

int arr[] = { 4, 5, 6 };

int n = sizeof(arr) / sizeof(arr[0]);

int k = 5;

cout << MinOperation(arr, n, k);

return 0;

}

[/sourcecode]

**Output:**

2

## Recommended Posts:

- Minimum operations to make XOR of array zero
- Minimum gcd operations to make all array elements one
- Minimum Increment operations to make Array unique
- Minimum no. of operations required to make all Array Elements Zero
- Minimum delete operations to make all elements of array same
- Minimum pair sum operations to make array each element divisible by 4
- Minimum operations required to make all the elements distinct in an array
- Minimum increment operations to make the array in increasing order
- Find minimum operations needed to make an Array beautiful
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum number of increment-other operations to make all array elements equal.
- Find minimum number of merge operations to make an array palindrome
- Minimum operations to make the MEX of the given set equal to x
- Minimum increment by k operations to make all elements equal
- Print modified array after multiple array range increment operations

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.