U
U
user_of_toster2021-03-15 16:45:34
Mathematics
user_of_toster, 2021-03-15 16:45:34

How to find the maximum number of sums equal to zero?

Task: given a list of numbers, -2, 1, 0, -1, 2, -4, 3, 1​. Find the maximum number of ways to choose numbers so that their sum is equal to zero. One in second place and one in fourth place are considered different numbers.

Example of combinations (number indexes starting from 1):

1, 5 (-2, 2 = 0)
1, 3, 5 (-2, 0, 2 = 0)

Where to dig? I understand that this is a task purely from combinatorics?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-03-15
@user_of_toster

If you need to derive the methods themselves, and not just their number, or the numbers can be very large (a couple of billions), then this is a task for exhaustive search. Or recursion, or iterate over the binary mask.
If the numbers themselves are not very large, and you only need to count the number of options, then a faster way is dynamic programming.
Count f(i,s) - how many ways to get the sum s using only the first i numbers.
Base F(0, 0) = 1, F(0,x) = 0.
Transition: F(i,s) = F(i-1, s) + F(i-1, sa[i-1]) ;
Answer: F(n,0).
Please note that the second parameter can also be negative.

D
Denis Ineshin, 2021-03-15
@IonDen

Well, for example Two Pointers Technique

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question