There are N players which are playing 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]

Which 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.

## C++

`// C/C++ program to find maximum number of ` `// games played by winner ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// method returns maximum games a winner needs ` `// to play in N-player tournament ` `int` `maxGameByWinner(` `int` `N) ` `{ ` ` ` `int` `dp[N]; ` ` ` ` ` `// for 0 games, 1 player is needed ` ` ` `// for 1 game, 2 players are required ` ` ` `dp[0] = 1; ` ` ` `dp[1] = 2; ` ` ` ` ` `// loop until i-th Fibonacci number is ` ` ` `// less than or equal to N ` ` ` `int` `i = 2; ` ` ` `do` `{ ` ` ` `dp[i] = dp[i - 1] + dp[i - 2]; ` ` ` `} ` `while` `(dp[i++] <= N); ` ` ` ` ` `// result is (i - 2) because i will be ` ` ` `// incremented one extra in while loop ` ` ` `// and we want the last value which is ` ` ` `// smaller than N, so one more decrement ` ` ` `return` `(i - 2); ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `N = 10; ` ` ` `cout << maxGameByWinner(N) << endl; ` ` ` `return` `0; ` `} ` |

## Java

`// Java program to find maximum number of ` `// games played by winner ` `class` `Max_game_winner { ` ` ` ` ` `// method returns maximum games a winner needs ` ` ` `// to play in N-player tournament ` ` ` `static` `int` `maxGameByWinner(` `int` `N) ` ` ` `{ ` ` ` `int` `[] dp = ` `new` `int` `[N]; ` ` ` ` ` `// for 0 games, 1 player is needed ` ` ` `// for 1 game, 2 players are required ` ` ` `dp[` `0` `] = ` `1` `; ` ` ` `dp[` `1` `] = ` `2` `; ` ` ` ` ` `// loop until i-th Fibonacci number is ` ` ` `// less than or equal to N ` ` ` `int` `i = ` `2` `; ` ` ` `do` `{ ` ` ` `dp[i] = dp[i - ` `1` `] + dp[i - ` `2` `]; ` ` ` `} ` `while` `(dp[i++] <= N); ` ` ` ` ` `// result is (i - 2) because i will be ` ` ` `// incremented one extra in while loop ` ` ` `// and we want the last value which is ` ` ` `// smaller than N, so one more decrement ` ` ` `return` `(i - ` `2` `); ` ` ` `} ` ` ` ` ` `// Driver code to test above methods ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `N = ` `10` `; ` ` ` `System.out.println(maxGameByWinner(N)); ` ` ` `} ` `} ` `//This code is contributed by Sumit Ghosh ` |

## Python3

# Python3 program to find maximum

# number of games played by winner

# method returns maximum games

# a winner needs to play in

# N-player tournament

def maxGameByWinner(N):

dp = [0 for i in range(N)]

# for 0 games, 1 player is needed

# for 1 game, 2 players are required

dp[0] = 1

dp[1] = 2

# loop until i-th Fibonacci

# number is less than or

# equal to N

i = 1

while dp[i] <= N:
i = i + 1
dp[i] = dp[i - 1] + dp[i - 2]
# result is (i - 1) because i will be
# incremented one extra in while loop
# and we want the last value which is
# smaller than N, so
return (i - 1)
# Driver code
N = 10
print(maxGameByWinner(N))
# This code is contributed
# by sahilshelangia
[tabby title="C#"]

`// C# program to find maximum number ` `// of games played by winner ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// method returns maximum games a ` ` ` `// winner needs to play in N-player ` ` ` `// tournament ` ` ` `static` `int` `maxGameByWinner(` `int` `N) ` ` ` `{ ` ` ` `int` `[] dp = ` `new` `int` `[N]; ` ` ` ` ` `// for 0 games, 1 player is needed ` ` ` `// for 1 game, 2 players are required ` ` ` `dp[0] = 1; ` ` ` `dp[1] = 2; ` ` ` ` ` `// loop until i-th Fibonacci number ` ` ` `// is less than or equal to N ` ` ` `int` `i = 2; ` ` ` ` ` `do` `{ ` ` ` `dp[i] = dp[i - 1] + dp[i - 2]; ` ` ` `} ` `while` `(dp[i++] <= N); ` ` ` ` ` `// result is (i - 2) because i will be ` ` ` `// incremented one extra in while loop ` ` ` `// and we want the last value which is ` ` ` `// smaller than N, so one more decrement ` ` ` `return` `(i - 2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 10; ` ` ` `Console.Write(maxGameByWinner(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

## PHP

`<?php ` `// PHP program to find maximum number ` `// of games played by winner ` ` ` `// Method returns maximum games ` `// a winner needs to play in ` `// N-player tournament ` `function` `maxGameByWinner(` `$N` `) ` `{ ` ` ` `$dp` `[` `$N` `]=0; ` ` ` ` ` `// for 0 games, 1 player is needed ` ` ` `// for 1 game, 2 players are required ` ` ` `$dp` `[0] = 1; ` ` ` `$dp` `[1] = 2; ` ` ` ` ` `// loop until i-th Fibonacci number is ` ` ` `// less than or equal to N ` ` ` `$i` `= 2; ` ` ` `do` ` ` `{ ` ` ` `$dp` `[` `$i` `] = ` `$dp` `[` `$i` `- 1] + ` `$dp` `[` `$i` `- 2]; ` ` ` `} ` `while` `(` `$dp` `[` `$i` `++] <= ` `$N` `); ` ` ` ` ` `// result is (i - 2) because i will be ` ` ` `// incremented one extra in while loop ` ` ` `// and we want the last value which is ` ` ` `// smaller than N, so one more decrement ` ` ` `return` `(` `$i` `- 2); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$N` `= 10; ` ` ` `echo` `maxGameByWinner(` `$N` `); ` ` ` `// This code is contributed by nitin mittal ` `?> ` |

**Output :**

4

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.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.

## Recommended Posts:

- Minimum Sum Path in a Triangle
- Size of The Subarray With Maximum Sum
- Maximum number of segments of lengths a, b and c
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Subset Sum Problem in O(sum) space
- Convert to Strictly increasing array with minimum changes
- Maximum sum of a path in a Right Number Triangle
- Maximum path sum in a triangle.
- Maximum subsequence sum such that no three are consecutive
- Maximum sum of pairs with specific difference
- Path with maximum average value
- Print Maximum Length Chain of Pairs
- Find number of solutions of a linear equation of n variables
- Maximum Length Chain of Pairs | DP-20
- Maximum size square sub-matrix with all 1s