human
Write me a code to kth Smallest Element of a Matrix of given dimensions filled with product of indices and the other steps required given the following:
Kth Smallest Element of a Matrix of given dimensions filled with product of indices | Function that returns true if the count of elements is less than mid.
To store count of elements less than mid.
Loop through each row.
Count elements less than mid in the ith row.
Function that returns the Kth smallest element in the NxM Matrix after sorting in an array.
Initialize low and high.
Perform binary search.
Find the mid.
Check if the count of elements is less than mid.
Return Kth smallest element of the matrix.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to kth Smallest Element of a Matrix of given dimensions filled with product of indices and the other steps required.
 def countLessThanMid ( mid , N , M , K ) :
	count = 0
	for i in range ( 1 , min ( N , mid ) + 1 ) :
		count = count + min ( mid // i , M )
	if ( count >= K ) :
		return False
	else :
		return True
def findKthElement ( N , M , K ) :
	low = 1
	high = N * M
	while ( low <= high ) :
		mid = low + ( high - low ) // 2
		if ( countLessThanMid ( mid , N , M , K ) ) :
			low = mid + 1
		else :
			high = mid - 1
	return high + 1
if __name__ == " _ _ main _ _ " :
	N = 2
	M = 3
	K = 5
	print ( findKthElement ( N , M , K ) )