Created
October 24, 2022 07:17
-
-
Save Tokiyomi/9a80a6093baf37423a9142f06a01150b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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