Minimum time required to schedule K processes | Function to execute k processes that can be gained in minimum amount of time ; Stores all the array elements ; Push all the elements to the priority queue ; Stores the required result ; Loop while the queue is not empty and K is positive ; Store the top element from the pq ; Add it to the answer ; Divide it by 2 and push it back to the pq ; Print the answer ; Driver Code
function executeProcesses ( A , N , K ) { let pq = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { pq . push ( A [ i ] ) ; } let ans = 0 ; pq . sort ( function ( a , b ) { return a - b ; } ) ; while ( pq . length > 0 && K > 0 ) { let top = pq . pop ( ) ; ans ++ ; K -= top ; top = Math . floor ( top / 2 ) ; pq . push ( top ) ; pq . sort ( function ( a , b ) { return a - b ; } ) ; } document . write ( ans ) } let A = [ 3 , 1 , 7 , 4 , 2 ] ; let K = 15 ; let N = A . length ; executeProcesses ( A , N , K ) ;