human
Can you provide a code to kth smallest element in an array that contains A [ i ] exactly B [ i ] times and the other steps required given the following:
Kth smallest element in an array that contains A [ i ] exactly B [ i ] times | Function to find the Kth smallest element that contains A [ i ] exactly B [ i ] times.
Traverse the given array.
Stores the frequency of every elements.
Traverse the given array.
Initialize a variable to store the prefix sums.
Iterate over the range [ 0 , M ].
Increment sum by freq [ i ].
If sum is greater than or equal to K.
Return the current element as answer.
Return - 1.
Driver Code.
Given Input.
Function call"
Solution in Python:
assistant
#Here is code to kth smallest element in an array that contains A [ i ] exactly B [ i ] times and the other steps required.
 def KthSmallest ( A , B , N , K ) :
	M = 0
	for i in range ( N ) :
		M = max ( A [ i ] , M )
	freq = [ 0 ] * ( M + 1 )
	for i in range ( N ) :
		freq [ A [ i ] ] += B [ i ]
	sum = 0
	for i in range ( M + 1 ) :
		sum += freq [ i ]
		if ( sum >= K ) :
			return i
	return - 1
if __name__ == " _ _ main _ _ " :
	A = [ 3 , 4 , 5 ]
	B = [ 2 , 1 , 3 ]
	N = len ( A )
	K = 4
	print ( KthSmallest ( A , B , N , K ) )