FastPrepCount Triplets Within a Value Range
Problems/Count Triplets Within a Value Range

Count Triplets Within a Value Range

IBM logoIBM● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report asks for array triplets whose maximum-minus-minimum value is bounded by the supplied threshold. The judged core matches the visible source at about 95%.

Given an integer array values and a nonnegative integer d, count index triplets i < j < k for which the difference between the maximum and minimum selected values is at most d.

Return the number of qualifying index triplets. Equal values at different indices are distinct choices.

Function

countBoundedTriplets(values: int[], d: int) → long

Examples

Example 1

values = [1,2,3,4]d = 2return = 2

The qualifying value groups use indices for [1,2,3] and [2,3,4].

Example 2

values = [1,1,1,1]d = 0return = 4

All four choices of three indices have range zero.

Example 3

values = [1,5,9]d = 3return = 0

The only triplet has range 8, which exceeds d.

Constraints

  • 3 <= values.length <= 200000.
  • -10^9 <= values[i] <= 10^9.
  • 0 <= d <= 2 * 10^9.
  • The answer fits in a signed 64-bit integer.

More IBM problems

  • Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
  • Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
  • Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public long countBoundedTriplets(int[] values, int d) {
        // Write your code here.
    }

}
Checking account…