human
kth smallest / largest in a small range unsorted array | Python 3 program of kth smallest / largest in a small range unsorted array.
Storing counts of elements.
Traverse hash array build above until we reach k - th smallest element ..
Driver Code"
How can the above be solved in Python?
 ... Give me a code to kth smallest / largest in a small range unsorted array and the other steps required?
assistant
#Here is code to kth smallest / largest in a small range unsorted array and the other steps required.
 def kthSmallestLargest ( arr , n , k ) :
	max_val = arr [ 0 ]
	for i in range ( len ( arr ) ) :
		if ( arr [ i ] > max_val ) :
			max_val = arr [ i ]
	hash = [ 0 for i in range ( max_val + 1 ) ]
	for i in range ( n ) :
		hash [ arr [ i ] ] += 1
	count = 0
	for i in range ( max_val + 1 ) :
		while ( hash [ i ] > 0 ) :
			count += 1
			if ( count == k ) :
				return i
			hash [ i ] -= 1
	return - 1
if __name__ == ' _ _ main _ _ ' :
	arr = [ 11 , 6 , 2 , 9 , 4 , 3 , 16 ]
	n = len ( arr )
	k = 3
	print ( " kth ▁ smallest ▁ number ▁ is : " , kthSmallestLargest ( arr , n , k ) )