Here is the code to find the combinations in the array.

n = int(input()) arr = [ int(input()) for i in range(n) ] teaMin = int(input()) sm = sum(arr) sums = set() sums.add(0) # Находим комбинации for item in arr: for k in range(sm, min(arr) - 1, -1): if (k - item) in sums: sums.add(k) print(sums) 

How to make it so that it finds a combination until a number is found that is divided into the teaMin variable without a remainder?

Let's say

[2,3,5]

The output should be a list:

[2,3,5,7,8,10,12,13,15 ... 32]

Tried through while, but could not figure out how to implement this task

  • one
    "Combinations" - in this case, what? - Enikeyschik
  • most likely in this code, because you do not know the upper limit of the nested loop - its search, in fact, is the essence of the problem. - tym32167
  • one
    @ Lerik Ashma. It is not necessary to start a new nickname for every question;) - MBo

1 answer 1

This is C #, not Pyton, but the idea seems to be clear.

 void Find(int[] data, int target) { var minValue = data.Min(); var maxValue = minValue * target; var sums = new HashSet<int>(); sums.Add(0); for (var i = minValue; i <= maxValue; i++) foreach (var item in data) if (sums.Contains(i - item)) { sums.Add(i); if (i % target == 0) { Console.WriteLine($"Result: {i}"); return; } } } 

for such input

 Find(new[] { 2, 3, 5 }, 16); 

It will issue

 Result: 16 

Since 16 is 8 times 2, that is, it is possible to make 16 of the original array, so this will be the minimum repetitive amount, which is divided by 16 without a remainder.

  • Elt with itself should not be folded. [2, 3, 5] - [(2 + 3) 5, (2 + 5) 7, ((2 + 3) + 5) 10 ... (until it is divided into the variable a that the user enters)] . Suppose a = 16, it turns out that the array should end in 32, because it is the minimum number a is divided into without a remainder. - Lerik Ashma
  • @LerikAshma how are you going to add up [2,3,5] without repeating to get 32 ? - tym32167
  • [2,3,5] - [... 8, ((2 + 3) + 5) 10, (10 + 2) 12, (10 + 3) 13, (10 + 5) 15, 17 (15+ 2), 18 (15 + 3), (15 + 5) 20 ... 32] - Lerik Ashma
  • @LerikAshma here ((2+3+5)+5)10 isn’t 5 not 2 times present? - tym32167
  • corrected, accidentally wrote 5 - Lerik Ashma