Organizing Tournament Problem
Given a positive integer N representing the count of players playing the game. The game is played between two teams such that each team consists of at least one player but the total count of players in the game must be N. The game lasts in exactly 30 minutes, the task is to check if all players will play the game against each other or not If the game can be played up to T hours and it is allowed to play the game more than 1 times. If found to be true then print “Possible”. Otherwise, print “Not Possible”.
Examples:
Input: N = 3, T = 1
Output: Possible
Explanation:
In 1st half hours Players { p1, p2 } played the game against { p3 }.
In 2d half hours Players { p2, P3 } played the game against { p1 }
Since all players played the game against each other within T(=1) hours. Therefore, the required output is “Possible”.
Input: N = 4, T = 0.5
Output: Not Possible
Explanation:
In 1st half hours Players { p1, p2 } played the game against { p3, p4 }.
Since player p1 did not play the game against p2 within T(=0.5) hours. Therefore, the required output is “Not Possible”.
Approach: The problem can be solved using Greedy technique. Following are the observations:
- In each game, if one of the two teams has only one player then the game must be played N – 1 times.
- In each game, If one of the team have N / 2 players and other team have (N + 1) / 2 then the game must be played (N + 1) / 2 times.
Follow the steps below to solve the problem:
- If total time to play the game N-1 times is less than or equal to T, then print “Possible”.
- If total time to play the game (N + 1) / 2 times is less than or equal to T, then print “Possible”.
- Otherwise, print “Not Possible”.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
string calculate( int N, int T)
{
if (N <= 1 || T <= 0) {
return "-1" ;
}
int minutes = T * 60;
int max_match = N - 1;
int max_time = max_match * 30;
if (max_time <= minutes) {
return "Possible" ;
}
int min_match = N / 2;
min_match = N - min_match;
int min_time = min_match * 30;
if (min_time <= minutes) {
return "Possible" ;
}
return "Not Possible" ;
}
int main()
{
int N = 6, T = 2;
cout << calculate(N, T);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String calculate( int N, int T)
{
if (N <= 1 || T <= 0 ) {
return "-1" ;
}
int minutes = T * 60 ;
int max_match = N - 1 ;
int max_time = max_match * 30 ;
if (max_time <= minutes) {
return "Possible" ;
}
int min_match = N / 2 ;
min_match = N - min_match;
int min_time = min_match * 30 ;
if (min_time <= minutes) {
return "Possible" ;
}
return "Not Possible" ;
}
public static void main(String[] args)
{
int N = 6 , T = 2 ;
System.out.println(calculate(N, T));
}
}
|
Python3
def calculate(N, T):
if N < = 1 or T < = 0 :
return - 1
minutes = T * 60
max_match = N - 1
max_time = max_match * 30
if max_time < = minutes:
return "Possible"
min_match = N / / 2
min_match = N - min_match
min_time = min_match * 30
if min_time < = minutes:
return "Possible"
return "Not Possible"
if __name__ = = "__main__" :
N = 6
T = 2
ans = calculate(N, T)
print (ans)
|
C#
using System;
class GFG{
static string calculate( int N, int T)
{
if (N <= 1 || T <= 0) {
return "-1" ;
}
int minutes = T * 60;
int max_match = N - 1;
int max_time = max_match * 30;
if (max_time <= minutes) {
return "Possible" ;
}
int min_match = N / 2;
min_match = N - min_match;
int min_time = min_match * 30;
if (min_time <= minutes) {
return "Possible" ;
}
return "Not Possible" ;
}
public static void Main(String[] args)
{
int N = 6, T = 2;
Console.WriteLine(calculate(N, T));
}
}
|
Javascript
<script>
function calculate(N, T)
{
if (N <= 1 || T <= 0)
{
return -1;
}
let minutes = T * 60;
let max_match = N - 1
max_time = max_match * 30
if (max_time <= minutes)
{
return "Possible" ;
}
min_match = Math.floor(N / 2);
min_match = N - min_match
min_time = min_match * 30;
if (min_time <= minutes)
{
return "Possible" ;
return "Not Possible" ;
}
}
let N = 6
let T = 2
let ans = calculate(N, T)
document.write(ans);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
12 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...