- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array called arr, this contains unique elements and we also have another value k. Now consider a game where we take first two elements of the array. In each turn, we compare arr[0] with arr[1], and the larger value wins and remains at position 0 and the smaller value moves to the end of the array. This game will end when a value wins’ k consecutive rounds. We have to find the winner from the array.

So, if the input is like arr = [1,5,6,3,4,2], and k = 3, then the output will be 6 because

round 1, arr = [1,5,6,3,4,2], winner 5, win count for 5 is 1

round 2, arr = [5,6,3,4,2,1], winner 6, win count for 6 is 1

round 3, arr = [6,3,4,2,1,5], winner 6, win count for 6 is 2

round 3, arr = [6,4,2,1,5,3], winner 6, win count for 6 is 3

So winner is 6 as it won three times (k = 3)

To solve this, we will follow these steps −

l := size of arr

prev := arr[0]

count := 0

for i in range 1 to l - 1, do

if prev > arr[i], then

count := count + 1

otherwise,

prev := arr[i]

count := 1

if count is same as k, then

return prev

return prev

Let us see the following implementation to get better understanding −

def solve(arr, k): l = len(arr) prev = arr[0] count = 0 for i in range(1, l): if prev > arr[i]: count+=1 else: prev = arr[i] count = 1 if count == k: return prev return prev arr = [1,5,6,3,4,2] k = 3 print(solve(arr, k))

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

6

- Related Questions & Answers
- Program to find winner of array removal game in Python
- Program to find winner of stone game in Python
- Program to find winner of number reducing game in Python
- Program to find winner of a rower breaking game in Python
- Program to find winner of a rower reducing game in Python
- Python program to find score and name of winner of minion game
- Program to find winner of a set element removal game in Python
- Program to find sign of the product of an array using Python
- Find the winner of a game where scores are given as a binary string in Python
- Predict the winner in Coin Game in C++
- Program to find maximum score of brick removal game in Python
- Program to find array of doubled pairs using Python
- Find the winner by adding Pairwise difference of elements in the array until Possible in Python
- Python Program to find the sum of array
- Python Program to find the largest element in an array

Advertisements