Minimum Distances
easy⏱ 15 mintypescriptchallengesproblem-solving-basic-
Runs in your browser
- Before starting
- Use a supported browser; initial dependencies may need internet.
- What it proves
- Run shows output; Test checks the authored cases, not every possible input.
- When finished
- Stop active runs when finished. No cloud infrastructure is provisioned by this exercise.
LEARN BY DOINGPut your intuition to work3 steps
Minimum Distances
Use the lesson’s examples and explanation to check your idea.
Personal reflection. Nothing is sent to an AI service.Before you start
Browser lesson checks; review each requirement in Tests.
Catalog level: Easy · Estimated time: 15 min. Your prior experience can change the difficulty.
Sample case — your program reads stdin and prints to stdout.
Need a hand?
Start with a small test case. Hints progress from an idea to an explanation and, when requested, code.
Starter Find the minimum distance between any two equal elements. Distance = absolute difference of their indices. If all elements are unique, return -1.
Example: a=[7,1,3,4,1,7] → Element 1 at indices 1 and 4 → distance=3. Element 7 at indices 0 and 5 → distance=5. Minimum is 3.
Input format
First line: n. Second line: n space-separated integers.
Output format
A single integer: the minimum distance, or -1 if all elements are unique.
Constraints
1 ≤ n ≤ 10³, 1 ≤ a[i] ≤ 10⁵