Skip to the content

The tournament jousts

Problem

Trumpets sound over the lists, and the herald goes over the rule of the jousts. The knights wait in a line, and the first two always ride against each other: the one with more strength wins. The winner stays at the front to face the next one, and the loser goes to the back of the line. The first knight to win k jousts in a row is crowned champion. Write a function that takes strengths, the knights' strengths in line order (positive whole numbers, all different, at least two), and k, a whole number from 1 to 100. Return the champion's strength. The joust in which someone takes the front spot already counts as the first of their streak. A large k is no problem: once the strongest knight reaches the front, he never loses and sooner or later gets his k wins. With [2, 1, 3, 5, 4, 6, 7] and k = 2: 2 beats 1 (1 win) and 1 goes to the back; 3 beats 2 (3 has 1 win); 5 beats 3 (1 win) and then 4 (2 wins). The champion is 5.

Examples

  • The example

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

  • One win is enough

    [6, 2, 9], 1 → 6

  • Two streaks that get cut short

    [3, 2, 1, 6, 5, 4, 9, 8, 7], 3 → 6

  • Only two knights

    [5, 8], 3 → 8

  • The first one keeps the front

    [9, 1, 2, 3], 3 → 9

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def champion(strengths, k):
    pass

JavaScript

function champion(strengths, k) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →