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 ; Prthe maximum length ; Driver Code ; Given arr ; Function Call
def isPower ( x ) : NEW_LINE INDENT if ( x != 0 and ( x & ( x - 1 ) ) == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def maximumlength ( arr , N ) : NEW_LINE INDENT max_length = 0 ; NEW_LINE max_len_subarray = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( isPower ( arr [ i ] ) ) : NEW_LINE INDENT max_length += 1 ; NEW_LINE max_len_subarray = max ( max_length , max_len_subarray ) ; NEW_LINE DEDENT else : NEW_LINE INDENT max_length = 0 ; NEW_LINE DEDENT DEDENT print ( max_len_subarray ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 4 , 6 , 8 , 8 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE maximumlength ( arr , N ) ; NEW_LINE DEDENT