Answer the question
In order to leave comments, you need to log in
How to iterate over all combinations?
I know about combinations from here .
I now have such a task - there is a set of numbers . Of these, you need to choose a combination whose sum of digits is odd and the largest among odd combinations. What algorithm can iterate through all combinations? In which direction to dig?
Answer the question
In order to leave comments, you need to log in
Why iterate over all combinations?
Sort a set of numbers.
Take the specified number of the largest ones.
If their sum is even, replace the smallest one with the previous one from the list until the sum becomes odd.
n = [1, 5, 3, 4, 9, 7, 6]
k = [3]
n_sort = [1, 3, 4, 5, 6, 7, 9]
k_sum = 6 + 7 + 9 = 22 - even number
Change 6 to 5, k_sum = 5+ 7 + 9 = 21 - PROFIT!
1. Analysis. What are the appearance properties of odd numbers? H+H=H; LF+LF=H; LF+H=LF. Bingo! Based on the above, we need to add arbitrarily even numbers and an odd number of odd numbers.
2. Decide on the properties of the sets. How many digits? Are they numbers or numbers?
3. When the task is unambiguously and clearly set, it will not be difficult to first filter out sets that do not add up to an odd number, then those whose numbers / numbers are not high enough (above average, for example), and from the remaining set it is already simple search to find the largest sum.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question