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++ Program for the above approach #include <iostream> using namespace std;
// Function to find the N players // the game against each other or not string calculate( int N, int T)
{ // Base Case
if (N <= 1 || T <= 0) {
// Return -1 if not valid
return "-1" ;
}
// Converting hours into minutes
int minutes = T * 60;
// Calculating maximum games that
// can be played.
int max_match = N - 1;
// Time required for conducting
// maximum games
int max_time = max_match * 30;
// Checking if it is possible
// to conduct maximum games
if (max_time <= minutes) {
// Return possible
return "Possible" ;
}
// Calculating minimum games
int min_match = N / 2;
min_match = N - min_match;
// Time required for conducting
// minimum games
int min_time = min_match * 30;
// Checking if it is possible
// to conduct minimum games
if (min_time <= minutes) {
// Return possible
return "Possible" ;
}
// Return not possible if time
// is less than required time
return "Not Possible" ;
} // Driver Code
// Total count of players
int main()
{ int N = 6, T = 2;
// function call
cout << calculate(N, T);
return 0;
} // This code is contributed by Parth Manchanda |
// Java program for the above approach import java.io.*;
class GFG {
// Function to find the N players // the game against each other or not static String calculate( int N, int T)
{ // Base Case
if (N <= 1 || T <= 0 ) {
// Return -1 if not valid
return "-1" ;
}
// Converting hours into minutes
int minutes = T * 60 ;
// Calculating maximum games that
// can be played.
int max_match = N - 1 ;
// Time required for conducting
// maximum games
int max_time = max_match * 30 ;
// Checking if it is possible
// to conduct maximum games
if (max_time <= minutes) {
// Return possible
return "Possible" ;
}
// Calculating minimum games
int min_match = N / 2 ;
min_match = N - min_match;
// Time required for conducting
// minimum games
int min_time = min_match * 30 ;
// Checking if it is possible
// to conduct minimum games
if (min_time <= minutes) {
// Return possible
return "Possible" ;
}
// Return not possible if time
// is less than required time
return "Not Possible" ;
} // Driver code public static void main(String[] args)
{ int N = 6 , T = 2 ;
// function call
System.out.println(calculate(N, T));
} } // This code is contributed by sanjoy_62. |
# Python program for the above problem # Function to find the N players # the game against each other or not def calculate(N, T):
# Base Case
if N < = 1 or T < = 0 :
# Return -1 if not valid
return - 1
# Converting hours into minutes
minutes = T * 60
# Calculating maximum games that
# can be played.
max_match = N - 1
# Time required for conducting
# maximum games
max_time = max_match * 30
# Checking if it is possible
# to conduct maximum games
if max_time < = minutes:
# Return possible
return "Possible"
# Calculating minimum games
min_match = N / / 2
min_match = N - min_match
# Time required for conducting
# minimum games
min_time = min_match * 30
# Checking if it is possible
# to conduct minimum games
if min_time < = minutes:
# Return possible
return "Possible"
# Return not possible if time
# is less than required time
return "Not Possible"
# Driver Code if __name__ = = "__main__" :
# Total count of players
N = 6
# Given hours
T = 2
# Function call
ans = calculate(N, T)
# Print ans
print (ans)
|
// C# program for the above approach using System;
class GFG{
// Function to find the N players // the game against each other or not static string calculate( int N, int T)
{ // Base Case
if (N <= 1 || T <= 0) {
// Return -1 if not valid
return "-1" ;
}
// Converting hours into minutes
int minutes = T * 60;
// Calculating maximum games that
// can be played.
int max_match = N - 1;
// Time required for conducting
// maximum games
int max_time = max_match * 30;
// Checking if it is possible
// to conduct maximum games
if (max_time <= minutes) {
// Return possible
return "Possible" ;
}
// Calculating minimum games
int min_match = N / 2;
min_match = N - min_match;
// Time required for conducting
// minimum games
int min_time = min_match * 30;
// Checking if it is possible
// to conduct minimum games
if (min_time <= minutes) {
// Return possible
return "Possible" ;
}
// Return not possible if time
// is less than required time
return "Not Possible" ;
} // Driver Code public static void Main(String[] args)
{ int N = 6, T = 2;
// function call
Console.WriteLine(calculate(N, T));
} } // This code is contributed by splevel62. |
<script> // JavaScript Program for the above approach
// Function to find the N players
// the game against each other or not
function calculate(N, T)
{
// Base Case
if (N <= 1 || T <= 0)
{
// Return -1 if not valid
return -1;
}
// Converting hours into minutes
let minutes = T * 60;
// Calculating maximum games that
// can be played.
let max_match = N - 1
// Time required for conducting
// maximum games
max_time = max_match * 30
// Checking if it is possible
// to conduct maximum games
if (max_time <= minutes)
{
// Return possible
return "Possible" ;
}
// Calculating minimum games
min_match = Math.floor(N / 2);
min_match = N - min_match
// Time required for conducting
// minimum games
min_time = min_match * 30;
// Checking if it is possible
// to conduct minimum games
if (min_time <= minutes)
{
// Return possible
return "Possible" ;
// Return not possible if time
// is less than required time
return "Not Possible" ;
}
}
// Driver Code
// Total count of players
let N = 6
// Given hours
let T = 2
// Function call
let ans = calculate(N, T)
// Print ans
document.write(ans);
// This code is contributed by Potta Lokesh
</script>
|
Possible
Time Complexity: O(1)
Auxiliary Space: O(1)