Diferente pentru blog/meet-in-the-middle intre reviziile #64 si #65

Nu exista diferente intre titluri.

Diferente intre continut:

h2. 4sum
bq. Given an array of integers, find out if there are any four numbers such that their sum equals zero. For example ... (you can use the same number more than once).
bq. Given A, an array of integers, find out if there are any four numbers such that their sum equals zero. For example given A = [2, 3, 1, 0, -4, -1] a solution is 3 + 1 + 0 - 4 = 0 or 0 + 0 + 0 + 0 = 0 (you can use the same number more than once).
 
The naive algorithm tries all the possible combinations of four numbers. This solution takes $O(N^4^)$ time.
The naive algorithm is $O(N^4^)$ try all the possible combinations of four numbers.
A less naive algorithm brute forces through all $n^3^$ combinations of three numbers and then efficiently checks if their sum is in the original array using a hash table. This algorithm has $O(n^3^)$ complexity.
By now, you’ve probably started thinking how meet in the middle can be applied to this problem. The trick is to rewrite the equation as $a + b = -(c + d)$. Store all the $n^2^$ sums $a + b$ in a hash set $S$. Then iterate through all $n^2^$ combinations for c and d and check if $S$ contains -(c + d).

Nu exista diferente intre securitate.

Topicul de forum nu a fost schimbat.