Imagine you're a magician performing a card trick where you need to find matching pairs in the quickest way possible! ๐ฉโจ
You are given an integer array cards where cards[i] represents the value of the i-th card. A pair of cards are matching if they have the same value.
Your challenge is to return the minimum number of consecutive cards you have to pick up to guarantee you have a pair of matching cards among the picked cards. If it's impossible to have matching cards (all cards are unique), return -1.
Example: If you have cards [3,4,2,3,4,7], you could pick up cards from index 0 to 3 (that's 4 consecutive cards: [3,4,2,3]) to get a matching pair of 3's. But there's a better way - pick up from index 1 to 4 (that's 4 consecutive cards: [4,2,3,4]) to get matching 4's. The minimum is 4 consecutive cards.
Input & Output
Constraints
- 1 โค cards.length โค 105
- 0 โค cards[i] โค 106
- At least one duplicate must exist for a valid answer