给定一个数组和 k,我们需要找到使数组的 GCD 等于或 k 的倍数所需的最小操作。这里的操作意味着将数组元素增加或减少 1。
例子:
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.
这里我们必须让数组的 gcd 等于或等于 k,这意味着会有一些元素接近 k 或它的一些倍数的情况。所以,为了解决这个问题,我们只需要让每个数组值等于或多于 K。通过这样做,我们将实现我们的解决方案,就好像每个元素都是 k 的倍数,那么它的 GCD 将至少为 K。现在我们的下一个目标是在最小操作中转换数组元素,即最小递增和递减次数。这个增量或减量的最小值只能通过从 K 中获取每个数字的余数来知道,即我们必须获取余数值或 (k-remainder) 值,以它们中的最小值为准。
下面是这种方法的实现:
C++
// CPP program to make GCD of array a multiple
// 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 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 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
Python3
# 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 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 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
$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
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。