← Unit 2 · all exercises
Saved in this browser only

Count the pairs that add up

Count how many pairs of whole numbers from 1 to n add up exactly to the target. Count each pair once, so 1+3 and 3+1 are the same pair.

int countPairs(int n, int target)

Solution.java
Loading editor…
Results

Examples

  • countPairs(5, 6) → 21+5 and 2+4
  • countPairs(5, 100) → 0

There are also 4 hidden tests you can't see. Solve the problem, not the examples.