Pairs Hitting Target
Count the pairs of positions i < j for which nums[i] + nums[j] == target. Return an int. Hint: build the table of all pair sums with broadcasting, then keep only the part above the diagonal (np.triu with k=1, or compare index grids).
Example 1
nums [1,2,3,4]
target 5
returns 2
The pairs 1+4 and 2+3 work.
Constraints
1 <= len(nums) <= 30
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More NumPy problems
- Pairwise SumsMedium
- Path LengthMedium
- Rank of Each ScoreMedium
- Ranks Without GapsMedium
- Record HighsMedium
- Risk of a MixMedium
- Row SharesMedium
- Sort by First ColumnMedium