- 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 that has n elements from 1 to n in shuffled order. Another integer K is given. There are N peoples they are standing in a queue to play badminton. The first two players will go for playing, then the loser will go and place at the end of the queue. The winner will play with the next person from the queue and so on. They will play until someone wins K times consecutively. Then that player becomes the winner.

If the queue is like [2, 1, 3, 4, 5] and K = 2, then output will be 5. Now see the explanation −

(2, 1) plays, 2 wins, so 1 will be added into the queue, Queue is like [3, 4, 5, 1] (2, 3) plays, 3 wins, so 2 will be added into the queue, Queue is like [4, 5, 1, 2] (3, 4) plays, 4 wins, so 3 will be added into the queue, Queue is like [5, 1, 2, 3] (4, 5) plays, 5 wins, so 4 will be added into the queue, Queue is like [1, 2, 3, 4] (5, 1) plays, 5 wins, so 3 will be added into the queue, Queue is like [2, 3, 4, 1]

(2, 1) plays, 2 wins, so 1 will be added into the queue, Queue is like [3, 4, 5, 1]

(2, 3) plays, 3 wins, so 2 will be added into the queue, Queue is like [4, 5, 1, 2]

(3, 4) plays, 4 wins, so 3 will be added into the queue, Queue is like [5, 1, 2, 3]

(4, 5) plays, 5 wins, so 4 will be added into the queue, Queue is like [1, 2, 3, 4]

(5, 1) plays, 5 wins, so 3 will be added into the queue, Queue is like [2, 3, 4, 1]

as 5 wins two consecutive matches, then output is 5.

Begin if k >= n-1, then return n best_player := 0 win_count := 0 for each element e in arr, do if e > best_player, then best_player := e if e is 0th element, then win_count := 1 end if else increase win_count by 1 end if if win_count >= k, then return best player done return best player End

#include <iostream> using namespace std; int winner(int arr[], int n, int k) { if (k >= n - 1) //if K exceeds the array size, then return n return n; int best_player = 0, win_count = 0; //initially best player and win count is not set for (int i = 0; i < n; i++) { //for each member of the array if (arr[i] > best_player) { //when arr[i] is better than the best one, update best best_player = arr[i]; if (i) //if i is not the 0th element, set win_count as 1 win_count = 1; }else //otherwise increase win count win_count += 1; if (win_count >= k) //if the win count is k or more than k, then we have got result return best_player; } return best_player; //otherwise max element will be winner. } main() { int arr[] = { 3, 1, 2 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 2; cout << winner(arr, n, k); }

3

- Related Questions & Answers
- Get a single element from the array of results by index in MongoDB
- Single Element in a Sorted Array in C++
- Group by element in array JavaScript
- Python – Extract element from a list succeeded by K
- k-th missing element in sorted array in C++
- JavaScript Splitting string by given array element
- Program to check whether we can convert string in K moves or not using Python
- Print direction of moves such that you stay within the [-k, +k] boundary in C++
- Program to find ith element by rotating k times to right
- Program to check if array pairs are divisible by k or not using Python
- Unset an attribute from a single array element in MongoDB?
- k-th missing element in an unsorted array in C++
- Minimum number of given moves required to make N divisible by 25 using C++.
- Minimum Moves to Equal Array Elements in C++
- K’th Smallest/Largest Element in Unsorted Array in C++

Advertisements