Array element moved by k using single moves

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++

// CPP 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 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[0]);
    int k = 2;
    cout << winner(a, n, k);
    return 0;
}

Python3

# Python 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"


Output:

5

Time complexity : O(n)

This article is contributed by Striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.