Array element moved by k using single moves
Last Updated :
27 Jul, 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++
#include <iostream>
using namespace std;
int winner( int a[], int n, int k)
{
if (k >= n - 1)
return n;
int best = 0, times = 0;
for ( int i = 0; i < n; i++) {
if (a[i] > best) {
best = a[i];
if (i)
times = 1;
}
else
times += 1;
if (times >= k)
return best;
}
return best;
}
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
import java.io.*;
class GFG {
static int winner( int a[], int n, int k)
{
if (k >= n - 1 )
return n;
int best = 0 , times = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] > best) {
best = a[i];
if (i == 1 )
times = 1 ;
}
else
times += 1 ;
if (times >= k)
return best;
}
return best;
}
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));
}
}
|
Python3
def winner( a, n, k):
if k > = n - 1 :
return n
best = 0
times = 0
for i in range (n):
if a[i] > best:
best = a[i]
if i = = True :
times = 1
else :
times + = 1
if times > = k:
return best
return best
a = [ 2 , 1 , 3 , 4 , 5 ]
n = len (a)
k = 2
print (winner(a, n, k))
|
C#
using System;
class GFG {
static int winner( int [] a, int n, int k)
{
if (k >= n - 1)
return n;
int best = 0, times = 0;
for ( int i = 0; i < n; i++) {
if (a[i] > best) {
best = a[i];
if (i == 1)
times = 1;
}
else
times += 1;
if (times >= k)
return best;
}
return best;
}
public static void Main()
{
int [] a = { 2, 1, 3, 4, 5 };
int n = a.Length;
int k = 2;
Console.WriteLine(winner(a, n, k));
}
}
|
PHP
<?php
function winner( $a , $n , $k )
{
if ( $k >= $n - 1)
return $n ;
$best = 0; $times = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] > $best )
{
$best = $a [ $i ];
if ( $i )
$times = 1;
}
else
$times += 1;
if ( $times >= $k )
return $best ;
}
return $best ;
}
$a = array ( 2, 1, 3, 4, 5 );
$n = sizeof( $a );
$k = 2;
echo (winner( $a , $n , $k ));
?>
|
Javascript
<script>
function winner(a, n, k)
{
if (k >= n - 1)
return n;
let best = 0, times = 0;
for (let i = 0; i < n; i++)
{
if (a[i] > best)
{
best = a[i];
if (i)
times = 1;
}
else
times += 1;
if (times >= k)
return best;
}
return best;
}
let a = [ 2, 1, 3, 4, 5 ];
let n = a.length;
let k = 2;
document.write(winner(a, n, k));
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...