Minimum operations to make GCD of array a multiple of k
Last Updated :
17 Aug, 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++
// 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 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;
}
// This code is contributed by Sania Kumari Gupta
C
// 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 code
int 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
// 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
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
<?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
?>
JavaScript
<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 code
let 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>
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Similar Reads
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E
4 min read
Program to Find GCD or HCF of Two Numbers Given two numbers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4, 5, 10 an
15+ min read
Check if two numbers are co-prime or not Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples : Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-PrimeThe idea is simple, we find GCD of two numbers a
5 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Program to find LCM of two numbers LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 12, b = 18Output : 3636 is the smallest number divisible by both 12 and 18Input : a = 5, b = 11Output : 5555 is the smallest number divisible by both 5 and 11[Naive Approach] Using Conditional Loop This appro
8 min read
LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read
Find the other number when LCM and HCF given Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60
4 min read
Minimum insertions to make a Co-prime array Given an array of N elements, find the minimum number of insertions to convert the given array into a co-prime array. Print the resultant array also.Co-prime Array : An array in which every pair of adjacent elements are co-primes. i.e, gcd(a, b) = 1 . Examples : Input : A[] = {2, 7, 28}Output : 1Exp
6 min read
Find the minimum possible health of the winning player Given an array health[] where health[i] is the health of the ith player in a game, any player can attack any other player in the game. The health of the player being attacked will be reduced by the amount of health the attacking player has. The task is to find the minimum possible health of the winn
4 min read
Minimum squares to evenly cut a rectangle Given a rectangular sheet of length l and width w. we need to divide this sheet into square sheets such that the number of square sheets should be as minimum as possible.Examples: Input :l= 4 w=6 Output :6 We can form squares with side of 1 unit, But the number of squares will be 24, this is not min
4 min read