forked from mathewsaju23/Hackoctober2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum.py
38 lines (29 loc) · 773 Bytes
/
sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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