Chess Tournament

0

0 votes
Problem

Vincoder’s friend Ritik is a great chess player. Vincoder went to a chess tournament to see ritik playing.

He observed something there and he thinks he could predict the winner of the tournament and now he has made a bet with Ritik that he will predict the winner.

The tournament is being played as follows.

There are N players waiting in a queue to play chess. The first 2 players in the line will play against each other. The winner will play with next person in the queue and the loser will go to the end of the queue and the game will continue this way until any of the player wins k games in a row. This player who wins k games in a row will be the winner of the tournament.

When 2 players are playing against each other, in order to decide who will win Ritik has given vincoder the ratings of each player. In the game of two, the player with greater rating will win.

Inorder to keep it simple the ratings of all the players are distinct.

Vincoder got an emergency call and he has go back home. Can you solve his problem and help him to win the bet?

Input format

  • First line contains space seprated integers N and K.
  • N is number of players in queue and K number of winnings required to be the winner.
  • Second line contains N space separated integers r1,r2,r3rn. Where rirepresents rating of ith player.

Ouptut format                    

  • Single line containing rating of winner

Constraints

  • 2<=N<=500
  • 2<=k<=1012
  • 1<=ri<=N
Sample Input
6 2
5 2 3 6 1 4
Sample Output
5
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

First player with rating 5 and player with rating 2 plays and player with rating 5 wins. So payer with rating 2 will go at the end .

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

Then player with rating 5 again wins against player with rating 3. So number of continuous wins of player with rating 5 becomes 2 which is equal to k=2. So rating 5 is the answer.

Editor Image

?