Maximize count of 1 s in an array by repeated division of array elements by 2 at most K times | Function to count the maximum number of array elements that can be reduced to 1 by repeatedly dividing array elements by 2 ; Sort the array in ascending order ; Store the count of array elements ; Traverse the array ; Store the number of operations required to reduce arr [ i ] to 1 ; Decrement k by opr ; If k becomes less than 0 , then break out of the loop ; Increment cnt by 1 ; Print the answer ; Driver Code
function findMaxNumbers ( arr , n , k ) { arr . sort ( ) ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let opr = Math . ceil ( Math . log2 ( arr [ i ] ) ) ; k -= opr ; if ( k < 0 ) { break ; } cnt ++ ; } document . write ( cnt ) ; } let arr = [ 5 , 8 , 4 , 7 ] ; let N = arr . length ; let K = 5 ; findMaxNumbers ( arr , N , K ) ;