human
Maximum number of Perfect Numbers present in a subarray of size K | Function to check a number is Perfect Number or not.
Stores sum of divisors.
Find all divisors and add them.
If sum of divisors is equal to N.
Function to return maximum sum of a subarray of size K.
If k is greater than N.
Compute sum of first window of size K.
Compute sums of remaining windows by removing first element of previous window and adding last element of current window.
Return the answer.
Function to find all the perfect numbers in the array.
The given array is converted into binary array.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to maximum number of Perfect Numbers present in a subarray of size K and the other steps required.
 def isPerfect ( N ) :
	sum = 1
	for i in range ( 2 , N ) :
		if i * i > N :
			break
		if ( N % i == 0 ) :
			if ( i == N // i ) :
				sum += i
			else :
				sum += i + N // i
	if ( sum == N and N != 1 ) :
		return 1
	return 0
def maxSum ( arr , N , K ) :
	if ( N < K ) :
		print ( " Invalid " )
		return - 1
	res = 0
	for i in range ( K ) :
		res += arr [ i ]
	curr_sum = res
	for i in range ( K , N ) :
		curr_sum += arr [ i ] - arr [ i - K ]
		res = max ( res , curr_sum )
	return res
def max_PerfectNumbers ( arr , N , K ) :
	for i in range ( N ) :
		if isPerfect ( arr [ i ] ) :
			arr [ i ] = 1
		else :
			arr [ i ] = 0
	return maxSum ( arr , N , K )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 28 , 2 , 3 , 6 , 496 , 99 , 8128 , 24 ]
	K = 4
	N = len ( arr )
	print ( max_PerfectNumbers ( arr , N , K ) )