Skip to content
Onsite

Count Pairs with Sum K

EasyP2Coding

Return the number of pairs of indices (i, j) with i < j such that nums[i] + nums[j] == k.

Equal values at different indices form different pairs.

Example 1

Input
nums = [1,2,3,4], k = 5
Output
2
Why
(1,4) and (2,3).

Example 2

Input
nums = [1,1,1], k = 2
Output
3
Why
Three indices give three pairs of 1 + 1.

Example 3

Input
nums = [], k = 5
Output
0

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i], k <= 10^9

Hints

Run checks the 3 visible tests, Submit all 10.