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++
// 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 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; } |
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 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
# Python3 code to find # winner of game # function to find the winner def 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 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 // 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 // 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. ?> |
Javascript
<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 code let 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> |
5
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Please Login to comment...