FastPrepCount Index Pairs with a Target Sum

Count Index Pairs with a Target Sum

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Return the number of index pairs (i,j) with i < j and nums[i] + nums[j] == target. Equal values at different indices form distinct pairs.

Function

countTargetPairs(nums: int[], target: int) → long

Examples

Example 1

nums = [1,2,1,1,2]target = 3return = 6

Each of three 1s pairs with each of two 2s.

Constraints

  • 0 <= nums.length <= 2 * 10^5.
  • The answer fits in signed 64-bit range.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public long countTargetPairs(int[] nums, int target) {
  // Write your code here.
}
nums[1,2,1,1,2]
target3
expected6
Checking account…