Search insert position of K in a sorted array | Function to find insert position of K ; Lower and upper bounds ; Traverse the search space ; If K is found ; Return the insert position ; Driver Code
def find_index ( arr , n , B ) : NEW_LINE INDENT start = 0 NEW_LINE end = n - 1 NEW_LINE while start <= end : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if arr [ mid ] == K : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ mid ] < K : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT return end + 1 NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( find_index ( arr , n , K ) ) NEW_LINE