Minimum operations to make GCD of array a multiple of k - GeeksforGeeks

21 Apr, 2022
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 means either increment or decrements 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 the 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 has 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 some elements are near k or 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 at least 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 implementation of this approach: 
C++
C
Java
Python3
C#
PHP
Javascript
// CPP program to make GCD of array a multiple// 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 codeint main(){    int arr[] = { 4, 5, 6 };    int n = sizeof(arr) / sizeof(arr[0]);    int k = 5;    cout << MinOperation(arr, n, k);    return 0;} // This code is contributed by Sania Kumari Gupta
// C program to make GCD of array a multiple of k.#include <stdio.h> // Find minimum between two numbers.int min(int num1, int num2){    return (num1 > num2) ? num2 : num1;} 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 codeint main(){    int arr[] = { 4, 5, 6 };    int n = sizeof(arr) / sizeof(arr[0]);    int k = 5;    printf("%d", MinOperation(arr, n, k));    return 0;} // This code is contributed by Sania Kumari Gupta
// Java program to make GCD of array a multiple of k.import java.io.*; class GFG {    static 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 + Math.min(a[i] % k, k - a[i] % k);            else                // Else we only have one choice which is                // to increment the valueto make equal                // to k                result = result + k - a[i];        }        return result;    }     // Driver code    public static void main(String[] args)    {        int arr[] = { 4, 5, 6 };        int n = arr.length;        int k = 5;        System.out.println(MinOperation(arr, n, k));    }} // This code is contributed by Sania Kumari Gupta
# Python 3 program to make GCD# of array a multiple of k.def MinOperation(a, n, k):         result = 0         for i in range(n) :             ''' 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 and 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 codeif __name__ == "__main__":         arr = [ 4, 5, 6 ]    n = len(arr)    k = 5    print(MinOperation(arr, n, k)) # This code is contributed# by ChitraNayal
// C#  program to make GCD// of array a multiple of k.using System; public class GFG{         static 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 +                    Math.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         static public void Main (){        int []arr = {4, 5, 6};        int n = arr.Length;        int k = 5;        Console.WriteLine(MinOperation(arr, n, k));    }} // This code is contributed// by Tushil
<?php// PHP program to make// GCD of array a multiple// of k. function MinOperation($a, $n, $k){    $result = 0;         for ($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$arr = array(4, 5, 6);$n = sizeof($arr) /     sizeof($arr[0]);$k = 5;echo MinOperation($arr, $n, $k); // This code is contributed// by @ajit?>
<script>   // Javascript program to make// GCD of array a multiple// of k. function MinOperation(a, n, k){    let result = 0;         for (let 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 + Math.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 codelet arr = [4, 5, 6];let n = arr.length;let k = 5;document.write(MinOperation(arr, n, k)); // This code is contributed// by _saurabh_jaiswal </script>
2
 
jit_t
ukasp
sanskar27jain
_saurabh_jaiswal
arorakashish0911
krisania804
GCD-LCM
Arrays
Greedy
Mathematical
Arrays
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Write a program to print all permutations of a given string
Huffman Coding | Greedy Algo-3