Skip to the code
+25 XP

#29 Count Pairs That Fill the Row

medium

A layout tool places two cards side by side in a row that is target pixels wide. The card widths are in widths.

Write countFillingPairs(widths, target) that returns how many pairs of indexes i < j have widths[i] + widths[j] === target. Cards with the same width are still different cards.

Example 1

Input: widths = [3,5,2,5,3], target = 8
Output: 4

Each of the two 3s pairs with each of the two 5s: 4 pairs.

Example 2

Input: widths = [4,4,4,4], target = 8
Output: 6

Any two of the four 4s work, and there are 6 ways to pick two.

Constraints

  • 0 <= widths.length <= 2 * 10^5
  • -10^9 <= widths[i], target <= 10^9
  • The answer is at most 2^31 - 1.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).