This Hackerrank problem deals with combinations.
import sys def getMoneySpent(keyboards, drives, s): combo = -1 for k in keyboards: for d in drives: if (k+d) > combo and (k+d) <= s: combo = k+d return str(combo) s,n,m = input().strip().split(' ') s,n,m = [int(s),int(n),int(m)] keyboards = list(map(int, input().strip().split(' '))) drives = list(map(int, input().strip().split(' '))) # The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items moneySpent = getMoneySpent(keyboards, drives, s) print(moneySpent)
This problem is pretty straight forward. I used to for loops to cycle through each list.
Another way to do it might be to sort the list eliminate the clearly overly expensive items that are over s and delete those that are duplicates to cycle through less elements. This might help for very big lists.