This is an interview question:
Given a pile of cards, how to program to get card combinations whose sum is closest to 21?
Array : 2, 3, 4, 5, J, K
2, 4, 5, J
2, 4, 5, K
I have a naive solution:
check if two cards' sum is 21;
check if three cards' sum is 21;
check if four cards' sum is 21;
check if five cards' sum is 21;(Max number of cards in blackjack)
However, this is brute force solution.
Can anyone give me a hint to get a better solution? Thanks!