human
Write me a code to kth smallest element in the array using constant space when array can 't be modified and the other steps required given the following:
Kth smallest element in the array using constant space when array can 't be modified | Function to return the kth smallest element from the array.
Minimum and maximum element from the array.
Modified binary search.
To store the count of elements from the array which are less than mid and the elements which are equal to mid.
If mid is the kth smallest.
If the required element is less than mid.
If the required element is greater than mid.
Driver code"
Solution in Python:
assistant
#Here is code to kth smallest element in the array using constant space when array can 't be modified and the other steps required.
 def kthSmallest ( arr , k , n ) :
	low = min ( arr ) ;
	high = max ( arr ) ;
	while ( low <= high ) :
		mid = low + ( high - low ) // 2 ;
		countless = 0 ; countequal = 0 ;
		for i in range ( n ) :
			if ( arr [ i ] < mid ) :
				countless += 1 ;
			elif ( arr [ i ] == mid ) :
				countequal += 1 ;
		if ( countless < k and ( countless + countequal ) >= k ) :
			return mid ;
		elif ( countless >= k ) :
			high = mid - 1 ;
		elif ( countless < k and countless + countequal < k ) :
			low = mid + 1 ;
if __name__ == " _ _ main _ _ " :
	arr = [ 7 , 10 , 4 , 3 , 20 , 15 ] ;
	n = len ( arr ) ;
	k = 3 ;
	print ( kthSmallest ( arr , k , n ) ) ;