Problem 343053 · hard · Level 03 Linear Management & Searching

Pairs With Product At Most T

two pointers · sorted arrays · counting · case analysis

Given a list of integers nums sorted in non-decreasing order (it may contain negative numbers and zeros) and an integer t, return the number of index pairs (i, j) with i < j such that nums[i] * nums[j] <= t.

Examples

Input:  nums = [-3, -1, 0, 2, 4], t = 0
Output: 8
Explanation: only (-3,-1) = 3 and (2,4) = 8 exceed 0.

Input:  nums = [-2, -1, 1, 3], t = -2
Output: 3
Explanation: (-2,1) = -2, (-2,3) = -6 and (-1,3) = -3 qualify.

Constraints

  • 0 <= len(nums) <= 10**5, -10**6 <= nums[i] <= 10**6
  • -10**12 <= t <= 10**12
  • Target: O(n) time, O(n) extra space at most (an O(n^2) pair loop will time out).

Goals

  • Split a signed problem into sign classes that each behave monotonically
  • Count pairs under a monotone predicate with a converging scan
Starting Python…