Find the shortest consecutive segment that contains two equal card values.
Given an array of integers representing card values laid out in a row, choose a consecutive group of cards so that at least one value appears twice within that group. Return the minimum number of cards you must pick up.
If no value appears more than once in the entire array, the answer is -1.
Input Format
cards: an array of integers
The array represents the values of cards in order.
Output Format
Return the minimum length of a consecutive subarray containing at least one duplicate value, or -1 if no such subarray exists.
Constraints
1 <= cards.length- Card values are integers
- If every value is unique, return
-1
(Exact platform constraints are not provided in the source metadata.)
Example 1
Input
cards = [3,4,2,3,4,7]
Output
4
Explanation
The shortest valid consecutive segment is [3,4,2,3], which has length 4 and contains the value 3 twice.
Example 2
Input
cards = [1,2,3,4,5]
Output
-1
Explanation
All card values are distinct, so no consecutive group can contain a duplicate.
Show 1 more example
Example 3
Input
cards = [1,1]
Output
2
Explanation
The entire array already contains a duplicate value, so the minimum length is 2.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.