All practice tasks

Coding (Algo)

Count Measurement Pairs Below a Limit

Two pointers · Medium

A capacity report counts pairs of observed loads that could share a fixed budget. Each observation is distinct even when its value repeats. The report needs the number of qualifying pairs, not an enumeration of them.

Count unordered pairs of distinct input positions whose value sum is no greater than the target limit.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ nums: integer[], target: integer }

Fields

  • nums is an array of signed integers. Repeated values at different positions are separate entries.
  • target is an integer between -2000000000 and 2000000000.

Output

Return value

  • Return one integer count of index pairs i < j. Equality with target qualifies.
  • Negative values and repeated values are valid. A position cannot pair with itself.

Constraints

  • 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
  • All totals and counts fit exactly in a safe JSON integer.

Examples

Example 1

{
  "nums": [
    1,
    2,
    3
  ],
  "target": 4
}

Returns

2

Example 2

{
  "nums": [
    2,
    2,
    2
  ],
  "target": 4
}

Returns

3

Required complexity

O(n log(n + 1)) time and O(n) copy space

What this task practices

Count Measurement Pairs Below a Limit is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as medium difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.