Maximum number of Armstrong Numbers present in a subarray of size K | Function to calculate the value of x raised to the power y in O ( log y ) ; Base Case ; If the power y is even ; Otherwise ; Function to calculate the order of the number , i . e . count of digits ; Stores the total count of digits ; Iterate until num is 0 ; Function to check a number is an Armstrong Number or not ; Find the order of the number ; Check for Armstrong Number ; If Armstrong number condition is satisfied ; Utility function to find the maximum sum of a subarray of size K ; If k is greater than N ; Find the sum of first subarray of size K ; Find the sum of the remaining subarray ; Return the maximum sum of subarray of size K ; Function to find all the Armstrong Numbers in the array ; Traverse the array arr [ ] ; If arr [ i ] is an Armstrong Number , then replace it by 1. Otherwise , 0 ; Return the resultant count ; Driver Code
def power ( x , y ) : NEW_LINE INDENT if ( y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( y % 2 == 0 ) : NEW_LINE INDENT return power ( x , y // 2 ) * power ( x , y // 2 ) NEW_LINE DEDENT return x * power ( x , y // 2 ) * power ( x , y // 2 ) NEW_LINE DEDENT def order ( num ) : NEW_LINE INDENT count = 0 NEW_LINE while ( num ) : NEW_LINE INDENT count += 1 NEW_LINE num = num // 10 NEW_LINE DEDENT return count NEW_LINE DEDENT def isArmstrong ( N ) : NEW_LINE INDENT r = order ( N ) NEW_LINE temp = N NEW_LINE sum = 0 NEW_LINE while ( temp ) : NEW_LINE INDENT d = temp % 10 NEW_LINE sum += power ( d , r ) NEW_LINE temp = temp // 10 NEW_LINE DEDENT return ( sum == N ) NEW_LINE DEDENT def maxSum ( arr , N , K ) : NEW_LINE INDENT if ( N < K ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT res += arr [ i ] NEW_LINE DEDENT curr_sum = res NEW_LINE for i in range ( K , N , 1 ) : NEW_LINE INDENT curr_sum += arr [ i ] - arr [ i - K ] NEW_LINE res = max ( res , curr_sum ) NEW_LINE DEDENT return res NEW_LINE DEDENT def maxArmstrong ( arr , N , K ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] = isArmstrong ( arr [ i ] ) NEW_LINE DEDENT return maxSum ( arr , N , K ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 28 , 2 , 3 , 6 , 153 , 99 , 828 , 24 ] NEW_LINE K = 6 NEW_LINE N = len ( arr ) NEW_LINE print ( maxArmstrong ( arr , N , K ) ) NEW_LINE DEDENT