There are N players which are playing in a tournament. We need to find the maximum number of games the winner can play. In this tournament, two players are allowed to play against each other only if the difference between games played by them is not more than one.
Examples:
Input : N = 3
Output : 2
Maximum games winner can play = 2
Assume that player are P1, P2 and P3
First, two players will play let (P1, P2)
Now winner will play against P3,
making total games played by winner = 2
Input : N = 4
Output : 2
Maximum games winner can play = 2
Assume that player are P1, P2, P3 and P4
First two pairs will play lets (P1, P2) and
(P3, P4). Now winner of these two games will
play against each other, making total games
played by winner = 2
We can solve this problem by first computing minimum number of players required such that the winner will play x games. Once this is computed actual problem is just inverse of this. Now assume that dp[i] denotes minimum number of players required so that winner plays i games. We can write a recursive relation among dp values as,
dp[i + 1] = dp[i] + dp[i – 1] because if runner up has played (i – 1) games and winner has played i games and all players against which they have played the match are disjoint, total games played by winner will be addition of those two sets of players.
Above recursive relation can be written as dp[i] = dp[i – 1] + dp[i – 2]
This is same as the Fibonacci series relation, so our final answer will be the index of the maximal Fibonacci number which is less than or equal to given number of players in the input.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxGameByWinner( int N)
{
int dp[N];
if (N == 1){
return 0;
}
dp[0] = 1;
dp[1] = 2;
int i = 1;
while (dp[i++] < N){
dp[i] = dp[i - 1] + dp[i - 2];
}
if (dp[i-1] == N){
return (i - 1);
}
return (i - 2);
}
int main()
{
int N = 10;
cout << maxGameByWinner(N) << endl;
return 0;
}
|
Java
import java.io.*;
class Max_game_winner {
static int maxGameByWinner( int N)
{
int [] dp = new int [N];
if (N == 1 ) {
return 0 ;
}
dp[ 0 ] = 1 ;
dp[ 1 ] = 2 ;
int i = 1 ;
while (dp[i++] < N) {
dp[i] = dp[i - 1 ] + dp[i - 2 ];
}
if (dp[i - 1 ] == N) {
return (i - 1 );
}
return (i - 2 );
}
public static void main(String args[])
{
int N = 10 ;
System.out.println(maxGameByWinner(N));
}
}
|
Python3
def maxGameByWinner(N):
dp = [ 0 for i in range (N)]
if N = = 1 :
return 0
dp[ 0 ] = 1
dp[ 1 ] = 2
i = 1
while dp[i] < N:
i = i + 1
dp[i] = dp[i - 1 ] + dp[i - 2 ]
if dp[i] = = N:
return i
return (i - 1 )
N = 10
print (maxGameByWinner(N))
|
C#
using System;
class GFG {
static int maxGameByWinner( int N)
{
int [] dp = new int [N];
if (N == 1){
return 0;
}
dp[0] = 1;
dp[1] = 2;
int i = 1;
while (dp[i++] < N){
dp[i] = dp[i - 1] + dp[i - 2];
}
if (dp[i-1] == N){
return (i - 1);
}
return (i - 2);
}
public static void Main()
{
int N = 10;
Console.Write(maxGameByWinner(N));
}
}
|
PHP
<?php
function maxGameByWinner( $N )
{
$dp [ $N ]=0;
if ( $N == 1){
return 0;
}
$dp [0] = 1;
$dp [1] = 2;
$i = 1;
while ( $dp [ $i ++] < $N ){
$dp [ $i ] = $dp [ $i - 1] + $dp [ $i - 2];
}
if ( $dp [ $i -1] == $N ){
return ( $i - 1);
}
return ( $i - 2);
}
$N = 10;
echo maxGameByWinner( $N );
?>
|
Javascript
<script>
function maxGameByWinner(N)
{
let dp = new Array(N).fill(0);
if (N == 1){
return 0;
}
dp[0] = 1;
dp[1] = 2;
let i = 1;
while (dp[i++] < N){
dp[i] = dp[i - 1] + dp[i - 2];
}
if (dp[i-1] == N){
return (i - 1);
}
return (i - 2);
}
let N = 10;
document.write(maxGameByWinner(N));
</script>
|
Time Complexity: O(N) where N represents the total number of players.
Auxiliary Space: O(N)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Dec, 2022
Like Article
Save Article