D
D
Daniil Demidko2016-12-19 09:51:29
Mathematics
Daniil Demidko, 2016-12-19 09:51:29

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

3 answer(s)
A
Anton Fedoryan, 2016-12-19
@AnnTHony

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!

X
x67, 2016-12-19
@x67

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.

V
Vladislav, 2016-12-19
@RGV

In general, combinations can be bypassed with for or recursion.
But it is felt that the task should be solved easier, although it is difficult to judge by what is written in the question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question