Skip to main content
Back to problems
Leetcode
Medium
Arrays
Combinatorics
Count Good Triplets

Count the number of index triplets whose pairwise absolute differences stay within given bounds.

Acceptance 0%
Problem Statement

Problem

Given an array of integers nums and three integers a, b, and c, count how many triplets of indices (i, j, k) satisfy:

  • 0 <= i < j < k < nums.length
  • |nums[i] - nums[j]| <= a
  • |nums[j] - nums[k]| <= b
  • |nums[i] - nums[k]| <= c

Return the total number of such good triplets.

This is a straightforward counting problem: you need to examine ordered index triplets and test whether they meet all three value-difference constraints.

Input Format

  • An integer array nums
  • Three integers a, b, and c

Output Format

  • Return one integer: the number of good triplets

Constraints

  • 3 <= nums.length
  • The array contains integers
  • a, b, and c are non-negative integers
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [3,0,1,1,9,7], a = 7, b = 2, c = 3

Output

4

Explanation

The good triplets are indexed by (0,1,2), (0,1,3), (0,2,3), and (1,2,3).

Example 2

Input

nums = [1,1,2,2,3], a = 0, b = 0, c = 1

Output

0

Explanation

No ordered triplet of indices satisfies all three difference constraints.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.