Skip to content

Instantly share code, notes, and snippets.

@Tokiyomi
Created October 24, 2022 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tokiyomi/9a80a6093baf37423a9142f06a01150b to your computer and use it in GitHub Desktop.
Save Tokiyomi/9a80a6093baf37423a9142f06a01150b to your computer and use it in GitHub Desktop.
# Solve Sum in Python
def solve_sum(A, B):
"""
This function implements a greedy approach to minimize the sum difference
among two sets A and B
"""
C = sorted(A + B, reverse=True) # Join both subsets and sort them in reversed order
a_sum = 0
b_sum = 0
equal_sum_set = [] # A set containing N_1 numbers with equal sum as the N_2 numbers
for c in C:
if a_sum > b_sum:
equal_sum_set.append(c)
b_sum += c
else:
a_sum += c
return equal_sum_set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment