Maximum number of teams of size K possible with each player from different country
Last Updated :
07 Aug, 2021
Given an array arr[] consisting of N positive integers and a positive integer K such that there are N countries, each country has arr[i] players, the task is to find the maximum number of teams that can be formed by forming teams of size K such that each player in the team is from a different country.
Examples:
Input: N = 4, K = 3, arr[] = {4, 3, 5, 3}
Output: 5
Explanation:
Consider the countries are named A, B, C and D. The possible ways of forming the teams are {A, B, C}, {A, C, D}, {A, B, C}, {B, C, D}, {A, C, D} such that in each set there are no more than 1 person from a country.
Therefore, the total count teams formed is 5.
Input: N = 3, K = 2, arr[] = {2, 3, 4}
Output: 4
Explanation:
Consider the countries are named A, B, C and D. The possible ways of forming the teams are {B, C}, {B, C}, {A, C}, ({A, B} or {A, C} or {B, C}) such that in each set there are no more than 1 person from a country.
Therefore, the total count teams formed is 4.
Approach: The given problem can be solved by using the Binary Search, the idea is to perform the Binary Search on the number of teams that can be formed. Let this variable be T. For each value of T, check if it is possible to form T teams from the given list of players from each country. T teams can be formed if the sum of the minimum of arr[i] or T for all i over the range [0, N – 1] is greater than equal to T*K. Follow the below steps to solve this problem:
- Define a function, say isPossible(arr, mid, K) to check if a mid number of teams can be formed or not.
- Initialize the variable sum as 0 to store the sum of array elements.
- Iterate over a range [0, N] and perform the following tasks:
- Add the value of a minimum of mid or arr[i] to the variable sum.
- If the sum is greater than equal to mid*K, then return true. Otherwise, return false.
- Initialize the variables, say lb and ub as 0 and 1e9 as the lower and upper bound of the number of teams that can be formed.
- Iterate over a while loop till lb is less than equal to ub and perform the following steps:
- Initialize the variable, say mid as the average of ub and lb.
- Call the function isPossible(arr, mid, K) and if the function returns true, then check for the upper part of the range. Otherwise, check for the lower part of the range.
- After performing the above steps, print the value of mid as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool is_possible(vector< int >& teams,
int T, int k)
{
int sum = 0;
for ( int i = 0; i < teams.size(); i++) {
sum += min(T, teams[i]);
}
return (sum >= (T * k));
}
int countOfTeams(vector< int >& teams_list,
int N, int K)
{
int lb = 0, ub = 1e9;
while (lb <= ub) {
int mid = lb + (ub - lb) / 2;
if (is_possible(teams_list, mid, K)) {
if (!is_possible(
teams_list, mid + 1, K)) {
return mid;
}
else {
lb = mid + 1;
}
}
else {
ub = mid - 1;
}
}
return 0;
}
int main()
{
vector< int > arr = { 2, 3, 4 };
int K = 2;
int N = arr.size();
cout << countOfTeams(arr, N, K);
return 0;
}
|
Java
class GFG {
public static boolean is_possible( int [] teams, int T, int k)
{
int sum = 0 ;
for ( int i = 0 ; i < teams.length; i++) {
sum += Math.min(T, teams[i]);
}
return (sum >= (T * k));
}
public static int countOfTeams( int [] teams_list, int N, int K)
{
int lb = 0 ;
double ub = 1e9;
while (lb <= ub) {
int mid = lb + ( int ) (ub - lb) / 2 ;
if (is_possible(teams_list, mid, K)) {
if (!is_possible(teams_list, mid + 1 , K)) {
return mid;
}
else {
lb = mid + 1 ;
}
}
else {
ub = mid - 1 ;
}
}
return 0 ;
}
public static void main(String args[]) {
int [] arr = { 2 , 3 , 4 };
int K = 2 ;
int N = arr.length;
System.out.println(countOfTeams(arr, N, K));
}
}
|
Python3
def is_possible(teams, T, k):
sum = 0
for i in range ( len (teams)):
sum + = min (T, teams[i])
return ( sum > = (T * k))
def countOfTeams(teams_list, N, K):
lb = 0
ub = 1000000000
while (lb < = ub):
mid = lb + (ub - lb) / / 2
if (is_possible(teams_list, mid, K)):
if (is_possible(teams_list, mid + 1 , K) = = False ):
return mid
else :
lb = mid + 1
else :
ub = mid - 1
return 0
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 ]
K = 2
N = len (arr)
print (countOfTeams(arr, N, K))
|
C#
using System;
class GFG{
public static bool is_possible( int [] teams,
int T, int k)
{
int sum = 0;
for ( int i = 0; i < teams.Length; i++)
{
sum += Math.Min(T, teams[i]);
}
return (sum >= (T * k));
}
public static int countOfTeams( int [] teams_list,
int N, int K)
{
int lb = 0;
double ub = 1e9;
while (lb <= ub)
{
int mid = lb + ( int ) (ub - lb) / 2;
if (is_possible(teams_list, mid, K))
{
if (!is_possible(teams_list, mid + 1, K))
{
return mid;
}
else
{
lb = mid + 1;
}
}
else
{
ub = mid - 1;
}
}
return 0;
}
public static void Main(String[] args)
{
int [] arr = { 2, 3, 4 };
int K = 2;
int N = arr.Length;
Console.WriteLine(countOfTeams(arr, N, K));
}
}
|
Javascript
<script>
function is_possible(teams, T, k) {
let sum = 0;
for (let i = 0; i < teams.length; i++) {
sum += Math.min(T, teams[i]);
}
return sum >= T * k;
}
function countOfTeams(teams_list, N, K) {
let lb = 0,
ub = 1e9;
while (lb <= ub) {
let mid = Math.floor(lb + (ub - lb) / 2);
if (is_possible(teams_list, mid, K)) {
if (!is_possible(teams_list, mid + 1, K)) {
return mid;
}
else {
lb = mid + 1;
}
}
else {
ub = mid - 1;
}
}
return 0;
}
let arr = [2, 3, 4];
let K = 2;
let N = arr.length;
document.write(countOfTeams(arr, N, K));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...