human
Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M | Function to find the minimum length of the subarray to be changed ..
Stores the frequencies of array elements.
Stores the number of array elements that are present more than N / M times.
Iterate over the range.
Increment the frequency.
If the frequency of all array elements are already N / M.
Stores the resultant length of the subarray.
The left and right pointers.
Iterate over the range.
If the current element is.
If the value of c is 0 , then find the possible answer.
Iterate over the range.
If the element at left is making it extra.
Update the left pointer.
Update the right pointer.
Return the resultant length.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M and the other steps required.
 def minimumSubarray ( arr , n , m ) :
	mapu = [ 0 for i in range ( m + 1 ) ]
	c = 0
	for i in range ( n ) :
		mapu [ arr [ i ] ] += 1
		if ( mapu [ arr [ i ] ] == ( n // m ) + 1 ) :
			c += 1
	if ( c == 0 ) :
		return 0
	ans = n
	l = 0
	r = 0
	while ( r < n ) :
		mapu [ arr [ r ] ] -= 1
		if ( mapu [ arr [ r ] ] == ( n // m ) ) :
			c -= 1
		if ( c == 0 ) :
			while ( l <= r and c == 0 ) :
				ans = min ( ans , r - l + 1 )
				mapu [ arr [ l ] ] += 1
				if ( mapu [ arr [ l ] ] > ( n // m ) ) :
					c += 1
				l += 1
		r += 1
	return ans
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 1 , 2 , 1 , 1 , 2 ]
	M = 2
	N = len ( arr )
	print ( minimumSubarray ( arr , N , M ) )