Maximum number of teams of size K possible with each player from different country | 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
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 ) ) ;