Hash map · Easy
A reconciliation tool receives integer adjustments in a batch. It must count pairs of different entries whose amounts add to a requested total. Repeated amounts are separate entries and can produce several distinct pairs.
Return the number of index pairs i < j for which nums[i] + nums[j] equals target.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "nums": number[], "target": number }.
Fields
- nums contains signed integer amounts. target is a signed integer required total.
Output
Return value
- Return one integer count. The same index cannot be used twice.
- Equal-valued entries at different indexes remain distinct choices.
- Inputs of length zero or one return 0.
Constraints
- At most 100000 values, each an integer in [-1000000000,1000000000].
- target is an integer in [-2000000000,2000000000]. The count can exceed 32 bits but fits in Number.
Examples
Example 1
{
"nums": [
1,
1,
1
],
"target": 2
}Returns
3Choose any two of the three indexes.
Example 2
{
"nums": [
2
],
"target": 4
}Returns
0An entry cannot pair with itself.
Required complexity
O(n) expected time and O(n) extra space