📜  大小为 K 的子数组中存在的最大 Armstrong 数

📅  最后修改于: 2022-05-13 01:56:06.794000             🧑  作者: Mango

大小为 K 的子数组中存在的最大 Armstrong 数

给定一个由N个整数和一个正整数K组成的数组arr[] ,任务是找出在任何大小为K的子数组中出现的 Armstrong 数的最大计数。

例子:

朴素方法:解决给定问题的最简单方法是生成所有可能的大小为K的子数组,并为每个子数组计算阿姆斯壮数的数字。检查所有子数组后,打印获得的最大计数。

时间复杂度: O(N*K)
辅助空间: O(1)

高效方法:上述方法可以通过将每个数组元素如果是阿姆斯特朗数改为1 ,否则将数组元素改为0 ,然后在更新后的数组中找到大小为K的最大和子数组来优化。请按照以下步骤获取有效方法:

  • 遍历数组arr[] ,如果当前元素arr[i]是阿姆斯壮数,则将当前元素替换为1 。否则,将其替换为0
  • 完成上述步骤后,将大小为 K 的子数组的最大和打印为大小为K的子数组中阿姆斯壮数的最大计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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 digits
int 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 not
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) {
 
        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 K
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 = max(res, curr_sum);
    }
 
    // Return the maximum sum
    // of subarray of size K
    return res;
}
 
// Function to find all the
// Armstrong Numbers in the array
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 Code
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
// Java program for above approach
import 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 digits
static 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 not
static 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 K
static 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 array
static 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 Code
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));
}
}
 
// 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 digits
def 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 not
def 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 K
def 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 array
def 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 Code
if __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 approach
using 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 digits
static 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 not
static 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 K
static 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 array
static 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 Code
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));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
4

时间复杂度: O(N * d),其中 d 是任何数组元素中的最大位数。
辅助空间: O(N)