Maximum number of Armstrong Numbers present in a subarray of size K

Given an array arr[] consisting of N integers and a positive integer K, the task is to find the maximum count of Armstrong Numbers present in any subarray of size K.
Examples:
Input: arr[] = {28, 2, 3, 6, 153, 99, 828, 24}, K = 6
Output: 4
Explanation: The subarray {2, 3, 6, 153} contains only of Armstrong Numbers. Therefore, the count is 4, which is maximum possible.Input: arr[] = {1, 2, 3, 6}, K = 2
Output: 2
Naive Approach: The simplest approach to solve the given problem is to generate all possible subarrays of size K and for each subarray, count the numbers that are an Armstrong Number. After checking for all the subarrays, print the maximum count obtained.Â
Time Complexity: O(N*K)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by changing each array element to 1 if it is an Armstrong Number, Otherwise, changing the array elements to 0 and then find the maximum sum subarray of size K in the updated array. Follow the steps below for the efficient approach:
- Traverse the array arr[] and if the current element arr[i] is an Armstrong Number, then replace the current element with 1. Otherwise, replace it with 0.
- After completing the above step, print the maximum sum of a subarray of size K as the maximum count of Armstrong Number in a subarray of size K.
Below is the implementation of the above approach:
C++
// C++ program for the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to calculate the value of// x raised to the power y in O(log y)int power(int x, unsigned int y){    // Base Case    if (y == 0)        return 1;Â
    // If the power y is even    if (y % 2 == 0)        return power(x, y / 2)               * power(x, y / 2);Â
    // Otherwise    return x * power(x, y / 2)           * power(x, y / 2);}Â
// Function to calculate the order of// the number, i.e. count of digitsint order(int num){    // Stores the total count of digits    int count = 0;Â
    // Iterate until num is 0    while (num) {        count++;        num = num / 10;    }Â
    return count;}Â
// Function to check a number is// an Armstrong Number or notint isArmstrong(int N){    // Find the order of the number    int r = order(N);    int temp = N, sum = 0;Â
    // Check for Armstrong Number    while (temp) {Â
        int d = temp % 10;        sum += power(d, r);        temp = temp / 10;    }Â
    // If Armstrong number    // condition is satisfied    return (sum == N);}Â
// Utility function to find the maximum// sum of a subarray of size Kint maxSum(int arr[], int N, int K){Â Â Â Â // If k is greater than NÂ Â Â Â if (N < K) {Â Â Â Â Â Â Â Â return -1;Â Â Â Â }Â
    // Find the sum of first    // subarray of size K    int res = 0;    for (int i = 0; i < K; i++) {Â
        res += arr[i];    }Â
    // Find the sum of the    // remaining subarray    int curr_sum = res;Â
    for (int i = K; i < N; i++) {Â
        curr_sum += arr[i] - arr[i - K];        res = max(res, curr_sum);    }Â
    // Return the maximum sum    // of subarray of size K    return res;}Â
// Function to find all the// Armstrong Numbers in the arrayint maxArmstrong(int arr[], int N,                 int K){    // Traverse the array arr[]    for (int i = 0; i < N; i++) {Â
        // If arr[i] is an Armstrong        // Number, then replace it by        // 1. Otherwise, 0        arr[i] = isArmstrong(arr[i]);    }Â
    // Return the resultant count    return maxSum(arr, N, K);}Â
// Driver Codeint main(){    int arr[] = { 28, 2, 3, 6, 153,                  99, 828, 24 };    int K = 6;    int N = sizeof(arr) / sizeof(arr[0]);    cout << maxArmstrong(arr, N, K);Â
    return 0;} |
Java
// Java program for above approachimport java.util.*;Â
class GFG{Â
// Function to calculate the value of// x raised to the power y in O(log y)static int power(int x, int y){         // Base Case    if (y == 0)        return 1;Â
    // If the power y is even    if (y % 2 == 0)        return power(x, y / 2) *                power(x, y / 2);Â
    // Otherwise    return x * power(x, y / 2) *                power(x, y / 2);}Â
// Function to calculate the order of// the number, i.e. count of digitsstatic int order(int num){         // Stores the total count of digits    int count = 0;Â
    // Iterate until num is 0    while (num > 0)    {        count++;        num = num / 10;    }    return count;}Â
// Function to check a number is// an Armstrong Number or notstatic int isArmstrong(int N){         // Find the order of the number    int r = order(N);    int temp = N, sum = 0;Â
    // Check for Armstrong Number    while (temp > 0)     {        int d = temp % 10;        sum += power(d, r);        temp = temp / 10;    }Â
    // If Armstrong number    // condition is satisfied    if (sum == N)        return 1;             return 0;}Â
// Utility function to find the maximum// sum of a subarray of size Kstatic int maxSum(int[] arr, int N, int K){Â Â Â Â Â Â Â Â Â // If k is greater than NÂ Â Â Â if (N < K) Â Â Â Â {Â Â Â Â Â Â Â Â return -1;Â Â Â Â }Â
    // Find the sum of first    // subarray of size K    int res = 0;    for(int i = 0; i < K; i++)     {        res += arr[i];    }Â
    // Find the sum of the    // remaining subarray    int curr_sum = res;Â
    for(int i = K; i < N; i++)    {        curr_sum += arr[i] - arr[i - K];        res = Math.max(res, curr_sum);    }Â
    // Return the maximum sum    // of subarray of size K    return res;}Â
// Function to find all the// Armstrong Numbers in the arraystatic int maxArmstrong(int[] arr, int N,                        int K){         // Traverse the array arr[]    for(int i = 0; i < N; i++)    {                 // If arr[i] is an Armstrong        // Number, then replace it by        // 1. Otherwise, 0        arr[i] = isArmstrong(arr[i]);    }Â
    // Return the resultant count    return maxSum(arr, N, K);}Â
// Driver Codepublic static void main(String[] args){    int[] arr = { 28, 2, 3, 6, 153,                  99, 828, 24 };    int K = 6;    int N = arr.length;         System.out.println(maxArmstrong(arr, N, K));}}Â
// This code is contributed by hritikrommie. |
Python3
# Python 3 program for the above approachÂ
# Function to calculate the value of# x raised to the power y in O(log y)def power(x, y):    # Base Case    if (y == 0):        return 1Â
    # If the power y is even    if (y % 2 == 0):        return power(x, y // 2) * power(x, y // 2)Â
    # Otherwise    return x * power(x, y // 2) * power(x, y // 2)Â
# Function to calculate the order of# the number, i.e. count of digitsdef order(num):    # Stores the total count of digits    count = 0Â
    # Iterate until num is 0    while (num):        count += 1        num = num // 10Â
    return countÂ
# Function to check a number is# an Armstrong Number or notdef isArmstrong(N):    # Find the order of the number    r = order(N)    temp = N    sum = 0Â
    # Check for Armstrong Number    while (temp):        d = temp % 10        sum += power(d, r)        temp = temp // 10Â
    # If Armstrong number    # condition is satisfied    return (sum == N)Â
# Utility function to find the maximum# sum of a subarray of size Kdef maxSum(arr, N, K):Â Â Â Â # If k is greater than NÂ Â Â Â if (N < K):Â Â Â Â Â Â Â Â return -1Â
    # Find the sum of first    # subarray of size K    res = 0    for i in range(K):        res += arr[i]Â
    # Find the sum of the    # remaining subarray    curr_sum = resÂ
    for i in range(K,N,1):        curr_sum += arr[i] - arr[i - K]        res = max(res, curr_sum)Â
    # Return the maximum sum    # of subarray of size K    return resÂ
# Function to find all the# Armstrong Numbers in the arraydef maxArmstrong(arr, N, K):       # Traverse the array arr[]    for i in range(N):               # If arr[i] is an Armstrong        # Number, then replace it by        # 1. Otherwise, 0        arr[i] = isArmstrong(arr[i])Â
    # Return the resultant count    return maxSum(arr, N, K)Â
# Driver Codeif __name__ == '__main__':Â Â Â Â arr = [28, 2, 3, 6, 153,99, 828, 24]Â Â Â Â K = 6Â Â Â Â N = len(arr)Â Â Â Â print(maxArmstrong(arr, N, K))Â Â Â Â Â Â Â Â Â # This code is contributed by ipg2016107. |
C#
// C# program for above approachusing System;Â
class GFG{Â
// Function to calculate the value of// x raised to the power y in O(log y)static int power(int x, int y){         // Base Case    if (y == 0)        return 1;Â
    // If the power y is even    if (y % 2 == 0)        return power(x, y / 2) *                power(x, y / 2);Â
    // Otherwise    return x * power(x, y / 2) *                power(x, y / 2);}Â
// Function to calculate the order of// the number, i.e. count of digitsstatic int order(int num){         // Stores the total count of digits    int count = 0;Â
    // Iterate until num is 0    while (num > 0)    {        count++;        num = num / 10;    }    return count;}Â
// Function to check a number is// an Armstrong Number or notstatic int isArmstrong(int N){         // Find the order of the number    int r = order(N);    int temp = N, sum = 0;Â
    // Check for Armstrong Number    while (temp > 0)     {        int d = temp % 10;        sum += power(d, r);        temp = temp / 10;    }Â
    // If Armstrong number    // condition is satisfied    if (sum == N)        return 1;             return 0;}Â
// Utility function to find the maximum// sum of a subarray of size Kstatic int maxSum(int[] arr, int N, int K){Â Â Â Â Â Â Â Â Â // If k is greater than NÂ Â Â Â if (N < K) Â Â Â Â {Â Â Â Â Â Â Â Â return -1;Â Â Â Â }Â
    // Find the sum of first    // subarray of size K    int res = 0;    for(int i = 0; i < K; i++)     {        res += arr[i];    }Â
    // Find the sum of the    // remaining subarray    int curr_sum = res;Â
    for(int i = K; i < N; i++)    {        curr_sum += arr[i] - arr[i - K];        res = Math.Max(res, curr_sum);    }Â
    // Return the maximum sum    // of subarray of size K    return res;}Â
// Function to find all the// Armstrong Numbers in the arraystatic int maxArmstrong(int[] arr, int N,                        int K){         // Traverse the array arr[]    for(int i = 0; i < N; i++)    {                 // If arr[i] is an Armstrong        // Number, then replace it by        // 1. Otherwise, 0        arr[i] = isArmstrong(arr[i]);    }Â
    // Return the resultant count    return maxSum(arr, N, K);}Â
// Driver Codepublic static void Main(String[] args){    int[] arr = { 28, 2, 3, 6, 153,                  99, 828, 24 };    int K = 6;    int N = arr.Length;         Console.Write(maxArmstrong(arr, N, K));}}Â
// This code is contributed by shivanisinghss2110 |
Javascript
<script>Â
// Javascript program for the above approachÂ
// Function to calculate the value of// x raised to the power y in O(log y)function power(x, y){    // Base Case    if (y == 0)         return 1;         // If the power y is even    if (y % 2 == 0)        return power(x, Math.floor(y / 2)) *                power(x, Math.floor(y / 2));         // Otherwise    return x * power(x, Math.floor(y / 2)) *                power(x, Math.floor(y / 2));}Â
// Function to calculate the order of// the number, i.e. count of digitsfunction order(num) {         // Stores the total count of digits    let count = 0;         // Iterate until num is 0    while (num)     {        count++;        num = Math.floor(num / 10);    }    return count;}Â
// Function to check a number is// an Armstrong Number or notfunction isArmstrong(N){         // Find the order of the number    let r = order(N);    let temp = N,    sum = 0;         // Check for Armstrong Number    while (temp)    {        let d = temp % 10;        sum += power(d, r);        temp = Math.floor(temp / 10);    }         // If Armstrong number    // condition is satisfied    return sum == N;}Â
// Utility function to find the maximum// sum of a subarray of size Kfunction maxSum(arr, N, K) {         // If k is greater than N    if (N < K)     {        return -1;    }         // Find the sum of first    // subarray of size K    let res = 0;    for(let i = 0; i < K; i++)     {        res += arr[i];    }         // Find the sum of the    // remaining subarray    let curr_sum = res;         for(let i = K; i < N; i++)    {        curr_sum += arr[i] - arr[i - K];        res = Math.max(res, curr_sum);    }         // Return the maximum sum    // of subarray of size K    return res;}Â
// Function to find all the// Armstrong Numbers in the arrayfunction maxArmstrong(arr, N, K) {         // Traverse the array arr[]    for(let i = 0; i < N; i++)    {                 // If arr[i] is an Armstrong        // Number, then replace it by        // 1. Otherwise, 0        arr[i] = isArmstrong(arr[i]);    }         // Return the resultant count    return maxSum(arr, N, K);}Â
// Driver Codelet arr = [ 28, 2, 3, 6, 153, 99, 828, 24 ];let K = 6;let N = arr.length;Â
document.write(maxArmstrong(arr, N, K));Â
// This code is contributed by gfgkingÂ
</script> |
4
Â
Time Complexity: O(N * d), where d is the maximum number of digits in any array element.
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



