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

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: 5Explanation: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: 4Explanation: 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 using namespace std;bool is_possible(vector& 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& teams_list,                 int N, int K){        int lb = 0, ub = 1e9;        while (lb 