Mid/SeniorArrayHash Table

Minimum Distance Between Three Equal Elements II

You are given an integer array nums.

A tuple (i, j, k) of 3 distinct indices is good if nums[i] == nums[j] == nums[k].

The distance of a good tuple is abs(i - j) + abs(j - k) + abs(k - i), where abs(x) denotes the absolute value of x.

Return an integer denoting the minimum possible distance of a good tuple. If no good tuples exist, return -1.

Example 1
Inputnums = [1,2,1,1,3]
Output6
The minimum distance is achieved by the good tuple (0, 2, 3), whose distance is abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 6.
Example 2
Inputnums = [1,1,2,3,2,1,2]
Output8
The minimum distance is achieved by the good tuple (2, 4, 6), whose distance is abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 8.

Constraints

  • 1 <= n == nums.length <= 10^5
  • 1 <= nums[i] <= n

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate