Maximum number of teams of size K possible with each player from different country | C ++ program for the above approach ; Function to find if T number of teams can be formed or not ; Store the sum of array elements ; Traverse the array teams [ ] ; Required Condition ; Function to find the maximum number of teams possible ; Lower and Upper part of the range ; Perform the Binary Search ; Find the value of mid ; Perform the Binary Search ; Otherwise , update the search range ; Otherwise , update the search range ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }