Related Articles

# Array element moved by k using single moves

• Difficulty Level : Medium
• Last Updated : 05 Apr, 2021

Given a list of n integers containing numbers 1-n in a shuffled way and a integer K. N people are standing in a queue to play badminton. At first, the first two players in the queue play a game. Then the loser goes to the end of the queue, and the one who wins plays with the next person from the line, and so on. They play until someone wins k games consecutively. This player becomes the winner.

Examples :

```Input: arr[] = {2, 1, 3, 4, 5}
k = 2
Output: 5
Explanation:
2 plays with 1, 1 goes to end of queue.
2 plays with 3, 3 wins, 2 goes to end of queue.
3 plays with 4, so 3 goes to the end of the queue.
5 plays with everyone and wins as it is the
largest of all elements.

Input: arr[] = {3, 1, 2}
k = 2
Output: 3
Explanation :
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. 3 wins twice in a row.```

A naive approach is to run two nested for loops and check for every element which one is more from i to n being the first loop and the second being from i+1 to n and then from 0 to n-1 and count the number of continuous smaller elements and get the answer.
This will not be efficient enough as it takes O(n*n) .

An efficient approach will be to run a loop from 1 to n and keep track of best (or maximum element) so far and number of smaller elements than this maximum. If current best loose, initialize the greater value to the best and the count to 1, as the winner won 1 time already. If at any step it has won k times, you get your answer. But if k >= n-1, then the maximum number will be the only answer as it will the most number of times being the greatest. If while iterating you don’t find any player that has won k times, then the maximum number which is in the list will always be our answer.

Below is the implementation to the above approach

## C++

 `// C++ program to find winner of game``#include ``using` `namespace` `std;` `int` `winner(``int` `a[], ``int` `n, ``int` `k)``{``    ``// if the number of steps is more then``    ``// n-1,``    ``if` `(k >= n - 1)``        ``return` `n;` `    ``// initially the best is 0 and no of``    ``// wins is 0.``    ``int` `best = 0, times = 0;` `    ``// traverse through all the numbers``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// if the value of array is more``        ``// then that of previous best``        ``if` `(a[i] > best) {` `            ``// best is replaced by a[i]``            ``best = a[i];` `            ``// if not the first index``            ``if` `(i)``                ``times = 1; ``// no of wins is 1 now``        ``}` `        ``else``            ``times += 1; ``// if it wins` `        ``// if any position has more then k wins``        ``// then return``        ``if` `(times >= k)``            ``return` `best;``    ``}` `    ``// Maximum element will be winner because``    ``// we move smaller element at end and repeat``    ``// the process.``    ``return` `best;``}` `// driver program to test the above function``int` `main()``{``    ``int` `a[] = { 2, 1, 3, 4, 5 };``    ``int` `n = ``sizeof``(a) / ``sizeof``(a);``    ``int` `k = 2;``    ``cout << winner(a, n, k);``    ``return` `0;``}`

## Java

 `// Java program to find winner of game``import` `java.io.*;` `class` `GFG {` `    ``static` `int` `winner(``int` `a[], ``int` `n, ``int` `k)``    ``{``        ``// if the number of steps is more then``        ``// n-1,``        ``if` `(k >= n - ``1``)``            ``return` `n;` `        ``// initially the best is 0 and no of``        ``// wins is 0.``        ``int` `best = ``0``, times = ``0``;` `        ``// traverse through all the numbers``        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``// if the value of array is more``            ``// then that of previous best``            ``if` `(a[i] > best) {` `                ``// best is replaced by a[i]``                ``best = a[i];` `                ``// if not the first index``                ``if` `(i == ``1``)` `                    ``// no of wins is 1 now``                    ``times = ``1``;``            ``}` `            ``else` `                ``// if it wins``                ``times += ``1``;` `            ``// if any position has more then``            ``// k wins then return``            ``if` `(times >= k)``                ``return` `best;``        ``}` `        ``// Maximum element will be winner``        ``// because we move smaller element``        ``// at end and repeat the process.``        ``return` `best;``    ``}` `    ``// driver program to test the above function``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `a[] = { ``2``, ``1``, ``3``, ``4``, ``5` `};``        ``int` `n = a.length;``        ``int` `k = ``2``;` `        ``System.out.println(winner(a, n, k));``    ``}``}` `/*This code is contributed by Nikita Tiwari.*/`

## Python3

 `# Python3 code to find``# winner of game` `# function to find the winner``def` `winner( a, n, k):` `    ``# if the number of steps is``    ``# more then n-1``    ``if` `k >``=` `n ``-` `1``:``        ``return` `n``    ` `    ``# initially the best is 0``    ``# and no of wins is 0.``    ``best ``=` `0``    ``times ``=` `0``    ` `    ``# traverse through all the numbers``    ``for` `i ``in` `range``(n):``    ` `        ``# if the value of array is more``        ``# then that of previous best``        ``if` `a[i] > best:``        ` `            ``# best is replaced by a[i]``            ``best ``=` `a[i]``            ` `            ``# if not the first index``            ``if` `i ``=``=` `True``:``                ` `                ``# no of wins is 1 now``                ``times ``=` `1``        ``else``:``            ``times ``+``=` `1` `# if it wins``            ` `        ``# if any position has more``        ``# then k wins then return``        ``if` `times >``=` `k:``            ``return` `best``            ` `    ``# Maximum element will be winner``    ``# because we move smaller element``    ``# at end and repeat the process.``    ``return` `best``    ` `    ` `# driver code``a ``=` `[ ``2``, ``1``, ``3``, ``4``, ``5` `]``n ``=` `len``(a)``k ``=` `2``print``(winner(a, n, k))` `# This code is contributed by "Abhishek Sharma 44"`

## C#

 `// C# program to find winner of game``using` `System;` `class` `GFG {` `    ``static` `int` `winner(``int``[] a, ``int` `n, ``int` `k)``    ``{``        ` `        ``// if the number of steps is more``        ``// then n-1,``        ``if` `(k >= n - 1)``            ``return` `n;` `        ``// initially the best is 0 and no of``        ``// wins is 0.``        ``int` `best = 0, times = 0;` `        ``// traverse through all the numbers``        ``for` `(``int` `i = 0; i < n; i++) {` `            ``// if the value of array is more``            ``// then that of previous best``            ``if` `(a[i] > best) {` `                ``// best is replaced by a[i]``                ``best = a[i];` `                ``// if not the first index``                ``if` `(i == 1)` `                    ``// no of wins is 1 now``                    ``times = 1;``            ``}` `            ``else` `                ``// if it wins``                ``times += 1;` `            ``// if any position has more``            ``// then k wins then return``            ``if` `(times >= k)``                ``return` `best;``        ``}` `        ``// Maximum element will be winner``        ``// because we move smaller element``        ``// at end and repeat the process.``        ``return` `best;``    ``}` `    ``// driver program to test the above``    ``// function``    ``public` `static` `void` `Main()``    ``{``        ``int``[] a = { 2, 1, 3, 4, 5 };``        ``int` `n = a.Length;``        ``int` `k = 2;` `        ``Console.WriteLine(winner(a, n, k));``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 `= ``\$n` `- 1)``        ``return` `\$n``;` `    ``// initially the best is 0``    ``// and no. of wins is 0.``    ``\$best` `= 0; ``\$times` `= 0;` `    ``// traverse through all the numbers``    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++)``    ``{` `        ``// if the value of array is more``        ``// then that of previous best``        ``if` `(``\$a``[``\$i``] > ``\$best``)``        ``{` `            ``// best is replaced by a[i]``            ``\$best` `= ``\$a``[``\$i``];` `            ``// if not the first index``            ``if` `(``\$i``)``            ` `                ``// no of wins is 1 now``                ``\$times` `= 1;``        ``}` `        ``else``        ` `            ``// if it wins``            ``\$times` `+= 1;` `        ``// if any position has more then``        ``// k wins then return``        ``if` `(``\$times` `>= ``\$k``)``            ``return` `\$best``;``    ``}` `    ``// Maximum element will be winner``    ``// because we move smaller element``    ``// at end and repeat the process.``    ``return` `\$best``;``}` `// Driver Code``\$a` `= ``array``( 2, 1, 3, 4, 5 );``\$n` `= sizeof(``\$a``);``\$k` `= 2;``echo``(winner(``\$a``, ``\$n``, ``\$k``));` `// This code is contributed by Ajit.``?>`

## Javascript

 ``

Output :

`5`

Time complexity : O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up