Google Online Assessment (OA) - Pick Up Coupons

You are given a scratchcard consisting of a row of non-negative integers. To win the scratch game, you need to scratch at least 2 numbers of the same value. You can only scratch consecutive numbers from the card. Scratching each number costs 1 dollar. Return the minimum cost to win the game, or -1 if winning is not possible.

Examples

Example 1:

Input:

[1, 3, 4, 2, 3, 4, 5]

Output: 4
Explanation:

You can scratch either [3, 4, 2, 3] or [4, 2, 3, 4].

Example 2:

Input:

[2, 5, 0, 3, 7]

Output: -1
Explanation:

There are no two numbers of the same value.

Try it yourself

Solution

Invest in Yourself

Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.

Go Pro