Problem · Array

Find the Winner of an Array Game

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEOA

Problem statement

Distinct integers in arr play repeated rounds. Compare the first two values: the larger remains at the front, and the smaller moves to the end. The round winner's consecutive-win count increases; a different winner starts a new streak at one.

Return the first value to win k consecutive rounds.

Function

arrayGameWinner(arr: int[], k: int) → int

Examples

Example 1

arr = [2,1,3,5,4,6,7]k = 2return = 5

Five defeats 3 and 4 in consecutive rounds.

Example 2

arr = [3,2,1]k = 10return = 3

Once the maximum reaches the front it can never lose.

Constraints

  • 2 <= arr.length <= 100000
  • 1 <= k <= 10^9
  • Values are distinct signed 32-bit integers.

More Goldman Sachs problems

drafts saved locally
public int arrayGameWinner(int[] arr, int k) {
  // write your code here
}
arr[2,1,3,5,4,6,7]
k2
expected5
checking account