Tap here to switch tabs
Problem
Submissions

Problem: Minimum Distance Between Three Equal Elements II

Minimum Distance Between Three Equal Elements II asks you to find three positions holding the same value and return the smallest sum of their pairwise distances, or -1 if none exist.

Statement

Given an integer array nums, a triplet of indices (i, j, k) is called a good tuple if:

  • 0≤0 \leq i << j << k << nums.length

  • nums[i] == nums[j] == nums[k]

The distance of a tuple (i, j, k) is calculated as abs(i - j) + abs(j - k) + abs(k - i).

Return the minimum distance among all possible good tuples. If no good tuple exists, return −1-1.

Constraints:

  • 1≤1 \leq n ==== nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤\leq n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Distance Between Three Equal Elements II

Minimum Distance Between Three Equal Elements II asks you to find three positions holding the same value and return the smallest sum of their pairwise distances, or -1 if none exist.

Statement

Given an integer array nums, a triplet of indices (i, j, k) is called a good tuple if:

  • 0≤0 \leq i << j << k << nums.length

  • nums[i] == nums[j] == nums[k]

The distance of a tuple (i, j, k) is calculated as abs(i - j) + abs(j - k) + abs(k - i).

Return the minimum distance among all possible good tuples. If no good tuple exists, return −1-1.

Constraints:

  • 1≤1 \leq n ==== nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤\leq n