Minimum time required to schedule K processes | Function to find minimum required time to schedule all process ; Stores max element from A [ ] ; Find the maximum element ; Stores frequency of each element ; Stores minimum time required to schedule all process ; Count frequencies of elements ; Find the minimum time ; Decrease the value of K ; Increment tmp [ i / 2 ] ; Increment the count ; Return count , if all process are scheduled ; Increment count ; Return the count ; If it is not possible to schedule all process ; Driver Code
function minTime ( A , n , K ) { let max_ability = A [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { max_ability = Math . max ( max_ability , A [ i ] ) ; } let tmp = Array . from ( { length : max_ability + 1 } , ( _ , i ) => 0 ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { tmp [ A [ i ] ] ++ ; } for ( let i = max_ability ; i >= 0 ; i -- ) { if ( tmp [ i ] != 0 ) { if ( tmp [ i ] * i < K ) { K -= ( i * tmp [ i ] ) ; tmp [ ( i / 2 ) ] += tmp [ i ] ; count += tmp [ i ] ; if ( K <= 0 ) { return count ; } } else { if ( K % i != 0 ) { count += Math . floor ( K / i ) + 1 ; } else { count += Math . floor ( K / i ) ; } return count ; } } } return - 1 ; } let arr = [ 3 , 1 , 7 , 2 , 4 ] ; let N = arr . length ; let K = 15 ; document . write ( minTime ( arr , N , K ) ) ;