human
Minimum difference between adjacent elements of array which contain elements from each row of a matrix | Return smallest element greater than or equal to the current element ..
Return the minimum absolute difference adjacent elements of array.
arr = [ 0 for i in range ( R ) ] [ for j in range ( C ) ] Sort each row of the matrix ..
For each matrix element.
Search smallest element in the next row which is greater than or equal to the current element.
largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted ..
Driver Program"
How can the above be solved in Python?
assistant
#Here is code to minimum difference between adjacent elements of array which contain elements from each row of a matrix and the other steps required.
 def bsearch ( low , high , n , arr ) :
	mid = ( low + high ) / 2
	if ( low <= high ) :
		if ( arr [ mid ] < n ) :
			return bsearch ( mid + 1 , high , n , arr ) ;
		return bsearch ( low , mid - 1 , n , arr ) ;
	return low ;
def mindiff ( arr , n , m ) :
	for i in range ( n ) :
		sorted ( arr )
	ans = 2147483647
	for i in range ( n - 1 ) :
		for j in range ( m ) :
			p = bsearch ( 0 , m - 1 , arr [ i ] [ j ] , arr [ i + 1 ] )
			ans = min ( ans , abs ( arr [ i + 1 ] [ p ] - arr [ i ] [ j ] ) )
			if ( p - 1 >= 0 ) :
				ans = min ( ans , abs ( arr [ i + 1 ] [ p - 1 ] - arr [ i ] [ j ] ) )
	return ans ;
m = [ 8 , 5 ] , [ 6 , 8 ]
print mindiff ( m , 2 , 2 )