Mind the Gap
InterviewTime limit2sMemory limit512 MB
Given n distinct positive card values, find an integer d so that the players, each playing their card exactly when its value is within d above the pile top and otherwise waiting, always place all cards in increasing order regardless of tie order.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Binary search, Array
- Solved
- No attempts yet
Problem
Mika is playing the Mind game with her friends.
The game is played with cards, and a single integer is written on each card. All values written on the cards are distinct. The players keep playing cards, building a single pile on the table. Initially the pile contains a single card with the integer on it. Each player is given a single card with a value from to . The players then start playing the cards in any order. When a player plays a card, they place it on top of the pile. The goal is to play all the cards so that the card values increase from the bottom to the top. If any player did not play their card, or the pile is not increasing, the players lose; otherwise they win. No communication between players is allowed after the cards are dealt.
Mika and her friends came up with a strategy. They agree on a single integer before the game. During the game, if a player's card value is and the top value of the pile is with , the player plays their card. If , the player does not play their card. If several players play their cards at the same time, these cards might be placed on top of the pile in any order, and this order is not controlled by the players.
You are given the card values that will be dealt to the players. Find an integer for the players' strategy that guarantees them a win.
Input
The first line contains an integer , the number of players in the Mind game ().
The second line contains integers, the card values dealt to the players.
All given card values are distinct, positive, and do not exceed .
Output
Print a single integer that Mika and her friends should use to guarantee a win in the game with their strategy. If no such exists, print . If several values of exist, print any of them.
Hint
In the first example, would also be a correct answer.