Array element moved by k using single moves - GeeksforGeeks

09 May, 2022
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++
Java
Python3
C#
PHP
Javascript
// C++ program to find winner of game#include <iostream>using namespace std; int winner(int a[], int n, int k){    // if the number of steps is more than    // 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        // than 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 than 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 functionint main(){    int a[] = { 2, 1, 3, 4, 5 };    int n = sizeof(a) / sizeof(a[0]);    int k = 2;    cout << winner(a, n, k);    return 0;}
// Java program to find winner of gameimport java.io.*; class GFG {     static int winner(int a[], int n, int k)    {        // if the number of steps is more than        // 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            // than 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 than            // 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 code to find# winner of game # function to find the winnerdef winner( a, n, k):     # if the number of steps is    # more than 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        # than 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        # than 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 codea = [ 2, 1, 3, 4, 5 ]n = len(a)k = 2print(winner(a, n, k)) # This code is contributed by "Abhishek Sharma 44"
// C# program to find winner of gameusing System; class GFG {     static int winner(int[] a, int n, int k)    {                 // if the number of steps is more        // than 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            // than 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            // than 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// PHP program to find winner of game function winner($a, $n, $k){    // if the number of steps    // is more than 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 = 0; $i < $n; $i++)    {         // if the value of array is more        // than 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 than        // 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.?>
<script> // Javascript program to find winner of game function winner(a, n, k){         // If the number of steps is more than    // n-1,    if (k >= n - 1)        return n;     // Initially the best is 0 and no of    // wins is 0.    let best = 0, times = 0;     // Traverse through all the numbers    for(let i = 0; i < n; i++)    {                 // If the value of array is more        // than 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        // than 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 codelet a = [ 2, 1, 3, 4, 5 ];let n = a.length;let k = 2; document.write(winner(a, n, k)); // This code is contributed by Mayank Tyagi     </script>
Output : 
5
Time complexity : O(n) 
vt_m
jit_t
mayanktyagi1709
simmytarika5
surinderdawra388
Arrays
Greedy
Arrays
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string