cswork / OAOpen practice workspace

AmazonOA #92

Pan Balance — Count Pair Groupings

Estimated MediumStatement and editorial — judging is being prepared

Topics (AI-labelled)HashingGreedy

A defective pan balance over-reads one side by a fixed amount wt. A pair of parcels (i, j) is "balanced" iff |weight[i] - weight[j]| == wt. Given weight[n] and wt, count the number of valid pairs. (One parcel cannot appear in two pairs; we count unordered pairs (i, j).) Return the count modulo 10⁹ + 7.

Constraints

  • 1 ≤ n ≤ 10⁵
  • 1 ≤ weight[i], wt ≤ 10⁹
Example:
weight = [4, 5, 5, 4, 2, 5, 3, 2, 3], wt = 1
countBalancedPairs(weight, 1) = 11
(every (4,5), (2,3), (3,2), (5,4) index-pair with |diff|==1)

Editorial

Loading solution…