First strictly smaller element in a sorted array in Java | Python3 program to find first element that is strictly smaller than given target ; Minimum size of the array should be 1 ; If target lies beyond the max element , than the index of strictly smaller value than target should be ( end - 1 ) ; Move to the left side if target is smaller ; Move right side ; Driver code
def next ( arr , target ) : NEW_LINE INDENT start = 0 ; NEW_LINE end = len ( arr ) - 1 ; NEW_LINE if ( end == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( target > arr [ end ] ) : NEW_LINE INDENT return end ; NEW_LINE DEDENT ans = - 1 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 ; NEW_LINE if ( arr [ mid ] >= target ) : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans = mid ; NEW_LINE start = mid + 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 , 8 , 12 ] ; NEW_LINE print ( next ( arr , 5 ) ) ; NEW_LINE DEDENT