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.
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.
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Coin game winner where every player has three choices
- Predict the winner of the game | Sprague-Grundy
- Maximum sum such that no two elements are adjacent
- Maximum size square sub-matrix with all 1s
- Maximum Sum Increasing Subsequence | DP-14
- Maximum Length Chain of Pairs | DP-20
- Maximum sum rectangle in a 2D matrix | DP-27
- Maximum Subarray Sum using Divide and Conquer algorithm
- Maximum Product Cutting | DP-36
- How to print maximum number of A's using given four keys
- Maximum profit by buying and selling a share at most twice
- Collect maximum points in a grid using two traversals
- Maximum weight transformation of a given string
- Collect maximum coins before hitting a dead end
- Maximum profit by buying and selling a share at most k times
- Find maximum length Snake sequence
- Maximum absolute difference between sum of two contiguous sub-arrays
- Printing Maximum Sum Increasing Subsequence
- Print Maximum Length Chain of Pairs
- Maximum weight path ending at any element of last row in a matrix