Maximum number of Armstrong Numbers present in a subarray of size K
Last Updated :
08 Sep, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int power( int x, unsigned int y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2)
* power(x, y / 2);
return x * power(x, y / 2)
* power(x, y / 2);
}
int order( int num)
{
int count = 0;
while (num) {
count++;
num = num / 10;
}
return count;
}
int isArmstrong( int N)
{
int r = order(N);
int temp = N, sum = 0;
while (temp) {
int d = temp % 10;
sum += power(d, r);
temp = temp / 10;
}
return (sum == N);
}
int maxSum( int arr[], int N, int K)
{
if (N < K) {
return -1;
}
int res = 0;
for ( int i = 0; i < K; i++) {
res += arr[i];
}
int curr_sum = res;
for ( int i = K; i < N; i++) {
curr_sum += arr[i] - arr[i - K];
res = max(res, curr_sum);
}
return res;
}
int maxArmstrong( int arr[], int N,
int K)
{
for ( int i = 0; i < N; i++) {
arr[i] = isArmstrong(arr[i]);
}
return maxSum(arr, N, K);
}
int 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
import java.util.*;
class GFG{
static int power( int x, int y)
{
if (y == 0 )
return 1 ;
if (y % 2 == 0 )
return power(x, y / 2 ) *
power(x, y / 2 );
return x * power(x, y / 2 ) *
power(x, y / 2 );
}
static int order( int num)
{
int count = 0 ;
while (num > 0 )
{
count++;
num = num / 10 ;
}
return count;
}
static int isArmstrong( int N)
{
int r = order(N);
int temp = N, sum = 0 ;
while (temp > 0 )
{
int d = temp % 10 ;
sum += power(d, r);
temp = temp / 10 ;
}
if (sum == N)
return 1 ;
return 0 ;
}
static int maxSum( int [] arr, int N, int K)
{
if (N < K)
{
return - 1 ;
}
int res = 0 ;
for ( int i = 0 ; i < K; i++)
{
res += arr[i];
}
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 res;
}
static int maxArmstrong( int [] arr, int N,
int K)
{
for ( int i = 0 ; i < N; i++)
{
arr[i] = isArmstrong(arr[i]);
}
return maxSum(arr, N, K);
}
public 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));
}
}
|
Python3
def power(x, y):
if (y = = 0 ):
return 1
if (y % 2 = = 0 ):
return power(x, y / / 2 ) * power(x, y / / 2 )
return x * power(x, y / / 2 ) * power(x, y / / 2 )
def order(num):
count = 0
while (num):
count + = 1
num = num / / 10
return count
def isArmstrong(N):
r = order(N)
temp = N
sum = 0
while (temp):
d = temp % 10
sum + = power(d, r)
temp = temp / / 10
return ( sum = = N)
def maxSum(arr, N, K):
if (N < K):
return - 1
res = 0
for i in range (K):
res + = arr[i]
curr_sum = res
for i in range (K,N, 1 ):
curr_sum + = arr[i] - arr[i - K]
res = max (res, curr_sum)
return res
def maxArmstrong(arr, N, K):
for i in range (N):
arr[i] = isArmstrong(arr[i])
return maxSum(arr, N, K)
if __name__ = = '__main__' :
arr = [ 28 , 2 , 3 , 6 , 153 , 99 , 828 , 24 ]
K = 6
N = len (arr)
print (maxArmstrong(arr, N, K))
|
C#
using System;
class GFG{
static int power( int x, int y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) *
power(x, y / 2);
return x * power(x, y / 2) *
power(x, y / 2);
}
static int order( int num)
{
int count = 0;
while (num > 0)
{
count++;
num = num / 10;
}
return count;
}
static int isArmstrong( int N)
{
int r = order(N);
int temp = N, sum = 0;
while (temp > 0)
{
int d = temp % 10;
sum += power(d, r);
temp = temp / 10;
}
if (sum == N)
return 1;
return 0;
}
static int maxSum( int [] arr, int N, int K)
{
if (N < K)
{
return -1;
}
int res = 0;
for ( int i = 0; i < K; i++)
{
res += arr[i];
}
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 res;
}
static int maxArmstrong( int [] arr, int N,
int K)
{
for ( int i = 0; i < N; i++)
{
arr[i] = isArmstrong(arr[i]);
}
return maxSum(arr, N, K);
}
public 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));
}
}
|
Javascript
<script>
function power(x, y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, Math.floor(y / 2)) *
power(x, Math.floor(y / 2));
return x * power(x, Math.floor(y / 2)) *
power(x, Math.floor(y / 2));
}
function order(num)
{
let count = 0;
while (num)
{
count++;
num = Math.floor(num / 10);
}
return count;
}
function isArmstrong(N)
{
let r = order(N);
let temp = N,
sum = 0;
while (temp)
{
let d = temp % 10;
sum += power(d, r);
temp = Math.floor(temp / 10);
}
return sum == N;
}
function maxSum(arr, N, K)
{
if (N < K)
{
return -1;
}
let res = 0;
for (let i = 0; i < K; i++)
{
res += arr[i];
}
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 res;
}
function maxArmstrong(arr, N, K)
{
for (let i = 0; i < N; i++)
{
arr[i] = isArmstrong(arr[i]);
}
return maxSum(arr, N, K);
}
let arr = [ 28, 2, 3, 6, 153, 99, 828, 24 ];
let K = 6;
let N = arr.length;
document.write(maxArmstrong(arr, N, K));
</script>
|
Time Complexity: O(N * d), where d is the maximum number of digits in any array element.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...