human
Find the only missing number in a sorted array | PYTHON 3 program to find the only missing element ..
If this is the first element which is not index + 1 , then missing element is mid + 1.
if this is not the first missing element search in left side.
if it follows index + 1 property then search in right side.
if no element is missing.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to find the only missing number in a sorted array and the other steps required.
 def findmissing ( ar , N ) :
	l = 0
	r = N - 1
	while ( l <= r ) :
		mid = ( l + r ) / 2
		mid = int ( mid )
		if ( ar [ mid ] != mid + 1 and ar [ mid - 1 ] == mid ) :
			return ( mid + 1 )
		elif ( ar [ mid ] != mid + 1 ) :
			r = mid - 1
		else :
			l = mid + 1
	return ( - 1 )
def main ( ) :
	ar = [ 1 , 2 , 3 , 4 , 5 , 7 , 8 ]
	N = len ( ar )
	res = findmissing ( ar , N )
	print ( res )
if __name__ == " _ _ main _ _ " :
	main ( )