- Related Questions & Answers
- Find the winner of a game where scores are given as a binary string in Python
- Program to find sign of the product of an array using Python
- Predict the winner in Coin Game in C++
- Find the winner by adding Pairwise difference of elements in the array until Possible in Python
- Python Program to find the sum of array
- Catching the ball game using Python
- Python Program to find the largest element in an array
- Java program to find the sum of elements of an array
- Program to find number of moves to win deleting repeated integer game in Python
- Find the sum of array in Python Program
- Predict the winner of the game on the basis of the absolute difference of sum by selecting numbers in C++
- Python Program to find largest element in an array
- C++ Program to Find the Minimum element of an Array using Binary Search approach
- C++ Program to Find the peak element of an array using Binary Search approach
- Program to find minimum operations to make the array increasing using Python

- 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

Advertisements