📜  使数组的GCD为k的倍数的最小运算

📅  最后修改于: 2021-05-06 19:46:14             🧑  作者: Mango

给定一个数组和k,我们需要找到使数组的GCD等于k或k的倍数所需的最小运算。这里的运算意味着将数组元素增加或减少1。
例子:

在这里,我们必须使数组的gcd等于k或等于k的倍数,这意味着在某些情况下某些元素接近k或接近k的某些倍数。因此,要解决这个问题,我们只需要使每个数组的值等于或等于K。通过这样做,我们将实现我们的解决方案,好像每个元素都是k的倍数,那么它的GCD至少应为K。现在我们的下一个目标是以最小的操作(即最小的增量和减量)转换数组元素。只能通过从K中取每个数字的余数来知道递增或递减的最小值,即我们必须取余数或(k余数)值中的最小者。
以下是此方法的实现:

C++
// CPP program to make GCD of array a mutiple
// of k.
#include 
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;
}


Java
// Java program to make GCD
// of array a mutiple 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 value
            // to 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 akt_mit


Python 3
# Python 3 program to make GCD
# of array a mutiple 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 code
if __name__ == "__main__":
     
    arr = [ 4, 5, 6 ]
    n = len(arr)
    k = 5
    print(MinOperation(arr, n, k))
 
# This code is contributed
# by ChitraNayal


C#
// C#  program to make GCD
// of array a mutiple 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
 $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
?>


Javascript


输出:
2