Fundamentals/Least Consecutive Cards to Match
← PrevNext →
Given an array of card values laid out in a line, find the minimum number of consecutive cards you must pick up so that the picked cards include a matching pair (two cards with the same value). If no matching pair exists anywhere, return -1.
Input
cards: array of integers representing card values
Output
The minimum length of a consecutive subarray containing a duplicate, or -1 if none exists.
Examples
Example 1
Inputcards = [3, 4, 2, 3, 4, 7]
Output4
Explanation: The subarray [3, 4, 2, 3] contains a matching pair of 3s.
Example 2
Inputcards = [1, 0, 5, 3]
Output-1
Constraints
Card values are between 0 and 10^6.
Return -1 if no duplicate exists.