How to find the combination of three elements in an array whose sum is equal to N in Python

1 Answer

0 votes
def PrintThreeElements(lst, N):
    for i in range(0, len(lst)):
        for j in range(i + 1, len(lst)):
            for k in range(j + 1, len(lst)):
                if lst[i] + lst[j] + lst[k] == N:
                    print(lst[i], lst[j], lst[k])
                    return

lst = [3, 2, 6, 4, 10, 5, 9, 7, 8, 12]

N = 24

PrintThreeElements(lst, N)


 
 
 
'''
run:
 
3 9 12

'''

 



answered Sep 6, 2022 by avibootz
...