Length of longest subarray with product equal to a power of 2 | Function to check whether a number is power of 2 or not ; Function to find maximum length subarray having product of element as a perfect power of 2 ; Stores current subarray length ; Stores maximum subarray length ; Traverse the given array ; If arr [ i ] is power of 2 ; Increment max_length ; Update max_len_subarray ; Otherwise ; Print the maximum length ; Given arr [ ] ; Function Call
function isPower ( x ) { return ( x && ( ! ( x & ( x - 1 ) ) ) ) ; } function maximumlength ( arr , N ) { var max_length = 0 ; var max_len_subarray = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( isPower ( arr [ i ] ) == 1 ) { max_length ++ ; max_len_subarray = Math . max ( max_length , max_len_subarray ) ; } else { max_length = 0 ; } } document . write ( max_len_subarray ) ; } var arr = [ 2 , 5 , 4 , 6 , 8 , 8 , 2 ] ; var N = arr . length ; maximumlength ( arr , N ) ;