Nested loops visit every ordered pair once. This brute-force approach is the baseline that clever hash-based solutions later improve on.
Count every pair of indices (i, j) with i < j where nums[i] + nums[j] equals target. Different index pairs count separately.
Examples
▸ nums = [1, 2, 3, 4, 5], target = 6
→ 2
▸ nums = [1, 1, 1], target = 2
→ 3
▸ nums = [], target = 5
→ 0
Progressive Hints
Hint 1 · Nudge
You have to consider every possible pairing exactly once.
Hint 2 · Plan
Use two nested loops so every pair of indexes (i, j) with i before j is visited exactly once. Bump a counter whenever the two values add up to target, and return the counter.
Hint 3 · Approach
count = 0. For i from 0 to the end: for j from i + 1 to the end: if nums[i] + nums[j] == target then count = count + 1. Return count.
All hints are out. Take a breath and give it a shot.