Maximum number of consecutive 1 's in binary representation of all the array elements | Function to return the count of maximum consecutive 1 s in the binary representation of x ; Initialize result ; Count the number of iterations to reach x = 0. ; This operation reduces length of every sequence of 1 s by one ; Function to return the count of maximum consecutive 1 s in the binary representation among all the elements of arr ; To store the answer ; For every element of the array ; Count of maximum consecutive 1 s in the binary representation of the current element ; Update the maximum count so far ; Driver code
function maxConsecutiveOnes ( x ) { var count = 0 ; while ( x != 0 ) { x = ( x & ( x << 1 ) ) ; count ++ ; } return count ; } function maxOnes ( arr , n ) { var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { var currMax = maxConsecutiveOnes ( arr [ i ] ) ; ans = Math . max ( ans , currMax ) ; } return ans ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; document . write ( maxOnes ( arr , n ) ) ;