Minimum Consecutive Cards to Pick Up

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 the cards have the same value.

Return the minimum number of consecutive cards you have to pick up to have a pair of matching cards among the picked cards. If it is impossible to have matching cards, return -1.

Example 1
Inputcards = [3,4,2,3,4,7]
Output4
We can pick up the cards [3,4,2,3] which contain a matching pair of cards with value 3; picking up [4,2,3,4] is also optimal.
Example 2
Inputcards = [1,0,5,3]
Output-1
There is no way to pick up a set of consecutive cards that contain a pair of matching cards.

Constraints

  • 1 <= cards.length <= 10^5
  • 0 <= cards[i] <= 10^6

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate