Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Combinatorics
Count Special Triplets

Count the number of special triplets in an array according to the problem's validity rule.

Acceptance 0%
Problem Statement

Problem

Given an integer array nums, count how many index triplets (i, j, k) satisfy the problem's special condition.

A triplet is valid only when the required ordering of indices holds and the values at those indices match the rule implied by the problem. Your task is to return the total number of valid triplets.

Because the answer may be large, return the count as an integer.

Notes

  • Triplets are based on indices, not values.
  • The same value may appear multiple times and can contribute to multiple triplets if the indices are distinct.
  • You should aim for an approach better than enumerating all O(n^3) triplets.

Input Format

  • An integer array nums.
  • The exact special condition determines which index triplets are counted.

Output Format

  • Return a single integer: the number of valid special triplets.

Constraints

  • 1 <= nums.length
  • Values are integers.
  • The result fits in a 64-bit signed integer for typical interview constraints.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 2, 1, 2, 1]

Output

4

Explanation

Illustrative example: there are four valid index triplets that satisfy the special condition for this array.

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.