Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Loading Cargo by laurentk310
def findMinRec(arr, i, sumCalculated, sumTotal):
# If we have reached last element.
# Sum of one subset is sumCalculated,
# sum of other subset is sumTotal-
# sumCalculated. Return absolute
# difference of two sums.
if (i == 0):
return abs((sumTotal - sumCalculated) - sumCalculated)
# For every item arr[i], we have two choices
# (1) We do not include it first set
# (2) We include it in first set
# We return minimum of two choices
return min(findMinRec(arr, i - 1, sumCalculated+arr[i - 1], sumTotal),
findMinRec(arr, i - 1, sumCalculated, sumTotal))
# Returns minimum possible difference between sums of two subsets
def findMin(arr, n):
# Compute total sum
# of elements
sumTotal = 0
for i in range(n):
sumTotal += arr[i]
# Compute result using
# recursive function
return findMinRec(arr, n, 0, sumTotal)
def checkio(data):
return findMin(data,len(data))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([10, 10]) == 0, "1st example"
assert checkio([10]) == 10, "2nd example"
assert checkio([5, 8, 13, 27, 14]) == 3, "3rd example"
assert checkio([5, 5, 6, 5]) == 1, "4th example"
assert checkio([12, 30, 30, 32, 42, 49]) == 9, "5th example"
assert checkio([1, 1, 1, 3]) == 0, "6th example"
July 2, 2021