# Maximum number of teams of size K possible with each player from different country

• Difficulty Level : Medium
• 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:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find if T number of teams``// can be formed or not``bool` `is_possible(vector<``int``>& teams,``                 ``int` `T, ``int` `k)``{``    ``// Store the sum of array elements``    ``int` `sum = 0;` `    ``// Traverse the array teams[]``    ``for` `(``int` `i = 0; i < teams.size(); i++) {``        ``sum += min(T, teams[i]);``    ``}` `    ``// Required Condition``    ``return` `(sum >= (T * k));``}` `// Function to find the maximum number``// of teams possible``int` `countOfTeams(vector<``int``>& teams_list,``                 ``int` `N, ``int` `K)``{``    ``// Lower and Upper part of the range``    ``int` `lb = 0, ub = 1e9;` `    ``// Perform the Binary Search``    ``while` `(lb <= ub) {` `        ``// Find the value of mid``        ``int` `mid = lb + (ub - lb) / 2;` `        ``// Perform the Binary Search``        ``if` `(is_possible(teams_list, mid, K)) {` `            ``if` `(!is_possible(``                    ``teams_list, mid + 1, K)) {``                ``return` `mid;``            ``}` `            ``// Otherwise, update the``            ``// search range``            ``else` `{``                ``lb = mid + 1;``            ``}``        ``}` `        ``// Otherwise, update the``        ``// search range``        ``else` `{``            ``ub = mid - 1;``        ``}``    ``}``    ``return` `0;``}` `// Driver Code``int` `main()``{``    ``vector<``int``> arr = { 2, 3, 4 };``    ``int` `K = 2;``    ``int` `N = arr.size();``    ``cout << countOfTeams(arr, N, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG {` `    ``// Function to find if T number of teams``    ``// can be formed or not``    ``public` `static` `boolean` `is_possible(``int``[] teams, ``int` `T, ``int` `k)``    ``{``      ` `        ``// Store the sum of array elements``        ``int` `sum = ``0``;` `        ``// Traverse the array teams[]``        ``for` `(``int` `i = ``0``; i < teams.length; i++) {``            ``sum += Math.min(T, teams[i]);``        ``}` `        ``// Required Condition``        ``return` `(sum >= (T * k));``    ``}` `    ``// Function to find the maximum number``    ``// of teams possible``    ``public` `static` `int` `countOfTeams(``int``[] teams_list, ``int` `N, ``int` `K)``    ``{``      ` `        ``// Lower and Upper part of the range``        ``int` `lb = ``0``;``        ``double` `ub = 1e9;` `        ``// Perform the Binary Search``        ``while` `(lb <= ub) {` `            ``// Find the value of mid``            ``int` `mid = lb + (``int``) (ub - lb) / ``2``;` `            ``// Perform the Binary Search``            ``if` `(is_possible(teams_list, mid, K)) {` `                ``if` `(!is_possible(teams_list, mid + ``1``, K)) {``                    ``return` `mid;``                ``}` `                ``// Otherwise, update the``                ``// search range``                ``else` `{``                    ``lb = mid + ``1``;``                ``}``            ``}` `            ``// Otherwise, update the``            ``// search range``            ``else` `{``                ``ub = mid - ``1``;``            ``}``        ``}``        ``return` `0``;``    ``}` `    ``// Driver Code``    ``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));` `    ``}``}` `// This code is contributed by _saurabh_jaiswal.`

## Python3

 `# Python 3 program for the above approach` `# Function to find if T number of teams``# can be formed or not``def` `is_possible(teams, T, k):``    ``# Store the sum of array elements``    ``sum` `=` `0` `    ``# Traverse the array teams[]``    ``for` `i ``in` `range``(``len``(teams)):``        ``sum` `+``=` `min``(T, teams[i])` `    ``# Required Condition``    ``return` `(``sum` `>``=` `(T ``*` `k))` `# Function to find the maximum number``# of teams possible` `def` `countOfTeams(teams_list, N, K):``    ``# Lower and Upper part of the range``    ``lb ``=` `0``    ``ub ``=` `1000000000` `    ``# Perform the Binary Search``    ``while` `(lb <``=` `ub):` `        ``# Find the value of mid``        ``mid ``=` `lb ``+` `(ub ``-` `lb) ``/``/` `2` `        ``# Perform the Binary Search``        ``if` `(is_possible(teams_list, mid, K)):``            ``if` `(is_possible(teams_list, mid ``+` `1``, K)``=``=``False``):``                ``return` `mid` `            ``# Otherwise, update the``            ``# search range``            ``else``:``                ``lb ``=` `mid ``+` `1` `        ``# Otherwise, update the``        ``# search range``        ``else``:``            ``ub ``=` `mid ``-` `1``    ``return` `0` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``2``, ``3``, ``4``]``    ``K ``=` `2``    ``N ``=` `len``(arr)``    ``print``(countOfTeams(arr, N, K))``    ` `    ``# This code is contributed by ipg2016107.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to find if T number of teams``// can be formed or not``public` `static` `bool` `is_possible(``int``[] teams,``                               ``int` `T, ``int` `k)``{``    ` `    ``// Store the sum of array elements``    ``int` `sum = 0;` `    ``// Traverse the array teams[]``    ``for``(``int` `i = 0; i < teams.Length; i++)``    ``{``        ``sum += Math.Min(T, teams[i]);``    ``}` `    ``// Required Condition``    ``return` `(sum >= (T * k));``}` `// Function to find the maximum number``// of teams possible``public` `static` `int` `countOfTeams(``int``[] teams_list,``                               ``int` `N, ``int` `K)``{``    ` `    ``// Lower and Upper part of the range``    ``int` `lb = 0;``    ``double` `ub = 1e9;` `    ``// Perform the Binary Search``    ``while` `(lb <= ub)``    ``{``        ` `        ``// Find the value of mid``        ``int` `mid = lb + (``int``) (ub - lb) / 2;` `        ``// Perform the Binary Search``        ``if` `(is_possible(teams_list, mid, K))``        ``{``            ``if` `(!is_possible(teams_list, mid + 1, K))``            ``{``                ``return` `mid;``            ``}` `            ``// Otherwise, update the``            ``// search range``            ``else``            ``{``                ``lb = mid + 1;``            ``}``        ``}` `        ``// Otherwise, update the``        ``// search range``        ``else``        ``{``            ``ub = mid - 1;``        ``}``    ``}``    ``return` `0;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int``[] arr = { 2, 3, 4 };``    ``int` `K = 2;``    ``int` `N = arr.Length;``    ` `    ``Console.WriteLine(countOfTeams(arr, N, K));``}``}` `// This code is contributed by code_hunt`

## Javascript

 ``
Output:
`4`

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up