Implement the method countPairsWithSum that counts index pairs whose values add up to a target.
Count pairs (i, j) where i < j and nums[i] + nums[j] == target.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizeelements. - Duplicate values can create multiple valid index pairs.
- Return the number of pairs, not the pairs themselves.
While scanning, add the number of earlier values equal to target - current, then record the current value.