4 Sum
4 Sum
LVM
Problem
GECKO
Algorithm
HOUSING
Java Program
RANK
4SUM
Problem
In this task, you are provided with four sets of integers. Your task
consists of selecting one integer from each set, such that their sum
is 0. You can assume that exactly one such selection exists.
Input File
3 2 4 2
5
17
-8
-13
19
6
-9
10
0
-14
7
Input File
3 2 4 2
5 |
17 |
-8 |
-13 |
19 |
6 |
-9 |
10 |
0 |
-14 |
7 |
Output File
17 -13 10 -14
Algorithm
Idea
In order for the overall sum to be 0, the sum of the first two values
must be minus the sum of the last two values.
Procedure
List the sums of all possible first two values, and the negatives of
the sums of all possible second two values.
Algorithm
Idea
In order for the overall sum to be 0, the sum of the first two values
must be minus the sum of the last two values.
Procedure
List the sums of all possible first two values, and the negatives of
the sums of all possible second two values. Then find the place
where the two lists have same value.
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Complexity
Complexity
Complexity
Complexity