Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question