Problem · Array

Chess Tournament

Learn this problem
EasyJPMorgan ChaseINTERNOA

Problem statement

The city of Hackerland organized a chess tournament for its citizens.

There are n participants numbered 1 to n where the ith participant has potential denoted by potential[i]. The potential of each player is distinct. Initially, all players stand in a queue in order from the 1st to the nth player In each game, the first 2 participants of the queue compete and the participant with a higher potential wins the game. After each game, the winner remains at the beginning of the queue and plays with the next person from the queue and the losing player goes to the end of the queue. The game continues until a player wins k consecutive games.

Given the potential of the participants and the deciding factor k, find the potential of the winning player.

Function

getPotentialOfWinner(potential: int[], k: long) → int

Complete the function getPotentialOfWinner in the editor below.

getPotentialOfWinner has the following parameters:

  1. int potential[n]: the potentials of participants
  2. long k: the number of consecutive matches the winning participant must win

Returns

int: the potential of the winning player

Examples

Example 1

potential = [3, 2, 1, 4]k = 2return = 3

Consider n = 4 participants have potential = [3, 2, 1, 4].

  • Initial position of participants: [1, 2, 3, 4].
  • Participants 1 and 2 compete. Their potentials are 3 and 2. Player 1 wins due to the higher potential. Player 1 stays at the front of the queue and player 2 moves to the back. Now their positions are [1, 3, 4, 2].
  • Participants 1 and 3 compete. Their potentials are 3 and 1. 1 wins a second consecutive game. Since k = 2, player 1 has won enough consecutive games.

Return player 1's potential, 3.

More JPMorgan Chase problems

drafts saved locally
public int getPotentialOfWinner(int[] potential, long k) {
  // write your code here
}
potential[3, 2, 1, 4]
k2
expected3
checking account