Minimum operations to make GCD of array a multiple of k | C # program to make GCD of array a multiple of k . ; 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 ; Else we only have one choice which is to increment the value to make equal to k ; Driver code
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 ( a [ i ] != 1 && a [ i ] > k ) { result = result + Math . Min ( a [ i ] % k , k - a [ i ] % k ) ; } else { result = result + k - a [ i ] ; } } return result ; } static public void Main ( ) { int [ ] arr = { 4 , 5 , 6 } ; int n = arr . Length ; int k = 5 ; Console . WriteLine ( MinOperation ( arr , n , k ) ) ; } }