Check if ceil of number divided by power of two exist in sorted array | Function to find there exist a number or not in the array ; Loop to check if there exist a number by divided by power of 2 ; Binary Search ; Condition to check the number is found in the array or not ; Otherwise divide the number by increasing the one more power of 2 ; Driver Code
def findNumberDivByPowerofTwo ( ar , k , n ) : NEW_LINE INDENT found = - 1 NEW_LINE m = k NEW_LINE while ( m > 0 ) : NEW_LINE INDENT l = 0 NEW_LINE r = n - 1 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if ( ar [ mid ] == m ) : NEW_LINE INDENT found = m NEW_LINE break NEW_LINE DEDENT elif ( ar [ mid ] > m ) : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT elif ( ar [ mid ] < m ) : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT if ( found != - 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT m = m // 2 NEW_LINE DEDENT return found NEW_LINE DEDENT arr = [ 3 , 5 , 7 , 8 , 10 ] NEW_LINE k = 4 NEW_LINE n = 5 NEW_LINE print ( findNumberDivByPowerofTwo ( arr , k , n ) ) NEW_LINE