407. Widest Equal Pair

A weather station logs one sensor reading per hour in the array nums. The analyst wants to find the longest stretch of time between two identical readings, no matter what the readings in between were.

Return the largest value of j - i over all index pairs i <= j such that nums[i] == nums[j]. A reading always matches itself, so the answer is never negative; it is 0 when no value occurs more than once (including when the array holds a single reading).

Example 1

Input:
nums = [3,8,1,3,9,8,3]
Output:
6
Explanation:

The value 3 occurs at indices 0 and 6, a distance of 6, which beats 8 (distance 4).

Example 2

Input:
nums = [4,5,6]
Output:
0
Explanation:

All readings are different, so the best distance is 0.

Constraints

1 ≤ nums.length ≤ 105

-109 ≤ nums[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…