Maximum distance between equal values
Given a list of integers, find the maximum distance between two equal values: the largest j - i over all index pairs i < j with nums[i] == nums[j]. Return 0 when no value occurs more than once (including empty and single-element lists). For example, in [3, 7, 3, 7, 3] the two outer 3s are 4 apart, so the answer is 4; in [4, 1, 4, 1, 1, 4] the answer is 5. A nested scan over all pairs is O(n^2) — one hash map and one pass is enough.
Implement
max_equal_spread(nums: list[int]) → intExamples
in
[[3,7,3,7,3]]out4What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 14 min
solution.py
InputExpectedGot
[[3,7,3,7,3]]4not run yetsample