Task 1 — Pair Sum Matching
Objective
Create a function that combines values from two integer slices by matching index positions.
For every resulting pair, calculate the sum of its two values.
Return the indexes of all pairs whose sum is equal to any target value provided in a third input slice.
Input
The function has three input arguments.
First Slice
p1 := []int{
-4, 5, -1, 3, 7,
-3, 6, -2, 4, 8,
10, 13, 17, 9, 2,
}
Second Slice
p2 := []int{
12, 7, 13, 8, 3,
14, 6, 15, 5, 4,
1, -3, -5, -2, 6,
}
Target Values
The third argument is:
[]int
and contains one or more target sums.
Example:
[]int{8, 10}
Pair Creation
Elements are paired using matching index positions.
For example:
Index 0 -> {-4, 12}
Index 1 -> {5, 7}
Index 2 -> {-1, 13}
...
For any valid index i:
pair = {p1[i], p2[i]}
and:
pairSum = p1[i] + p2[i]
Function
The function is named:
FindSum(...)
Conceptually:
FindSum(p1, p2, targets)
returns:
[]int
containing the matching pair indexes.
Requirements
For every index shared by p1 and p2:
- create the pair from
p1[i]andp2[i] - calculate the sum of the two values
- compare the sum with all target values
- if the sum matches any target, add index
ito the result
Each matching index should appear only once in the result.
Case 1
Call:
FindSum(p1, p2, []int{8, 10})
The function searches for pairs whose sums are either:
8
or
10
Expected Result
[]int{
0, 4, 11, 14,
}
Case 2
Call:
FindSum(p1, p2, []int{9, 11})
The function searches for pairs whose sums are either:
9
or
11
Expected Result
[]int{
3, 5, 8, 10,
}
Indexing
The expected results use standard zero-based slice indexes.
For example:
p1[0] = -4
p2[0] = 12
-4 + 12 = 8
Because 8 is one of the target values in Case 1, index:
0
is included in the result.
Input Validation
The two input slices represent correlated data and should therefore contain the same number of elements.
A robust implementation should validate that:
len(p1) == len(p2)
The original task does not define behavior for slices of different lengths.
The implementation should explicitly reject or otherwise handle such input rather than accidentally ignoring unmatched elements.
Implementation Notes
The implementation should work with other integer slices and target collections.
The function should not depend on the example length or values.
Consider how target lookup can be implemented efficiently when the third argument contains many possible target sums.