Open In App

Find largest Armstrong number in an Array

Last Updated : 11 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of size N. The task is to find the largest Armstrong number of the array.

Note: An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits in that number.

Examples:

Input: arr[] = [153, 9474, 1634, 371, 8208, 9475]
Output: 9474
Explanation: The largest Armstrong number in this array is 9474. 94+44+74+44 = 9474

Input: arr[] = [153, 238, 377, 369, 135]
Output: 153
Explanation: The largest Armstrong number in this array is 153. 13+53+33 = 153

Approach: To solve the problem follow the below idea:

We can solve this problem efficiently by iterating through the array and checking if each element is an Armstrong number. We can then keep track of the largest Armstrong number found so far.:

To solve the problem we follow the below algorithm:

  • Initialize a variable maxArmstrongNumber to 0.
  • Iterate through the array.
  • For each element in the array, calculate the sum of its digits raised to the power of the number of digits in that element.
    • If the sum is equal to the element, it is an Armstrong number.
    • If the element is an Armstrong number and greater than maxArmstrongNumber, update maxArmstrongNumber to that element.
  • Return maxArmstrongNumber as the largest Armstrong number in the array.

Below is the implementation of the above approach:

C++




// C++ program to find the largest
// armstrong number of the array
#include <bits/stdc++.h>
using namespace std;
 
int largest_Armstrong(int arr[], int n)
{
    int maxArmstrongNumber = 0;
 
    // Iterate through the array
    for (int i = 0; i < n; i++) {
        int number = arr[i];
        int sum = 0;
        int digits = 0;
        int temp = number;
 
        // Calculate the number of digits
        // in the element
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
 
        temp = number;
 
        // Calculate the sum of the digits
        // raised to the power of the
        // number of digits
        while (temp != 0) {
            int digit = temp % 10;
            sum += pow(digit, digits);
            temp /= 10;
        }
 
        // If the element is an Armstrong
        // number and greater than
        // maxArmstrongNumber, update
        // maxArmstrongNumber
        if (sum == number && number > maxArmstrongNumber) {
            maxArmstrongNumber = number;
        }
    }
 
    // Return the largest Armstrong
    // number in the array
    return maxArmstrongNumber;
}
 
// Drivers code
int main()
{
    int arr[] = { 153, 9474, 1634, 371, 8208, 9475 };
 
    // Calculate the number of elements
    // in the array
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "The largest Armstrong number in the array is: "
         << largest_Armstrong(arr, n) << endl;
    return 0;
}


Java




// Java program to find the largest armstrong number
// of the array
import java.util.*;
 
public class GFG {
    public static int largestArmstrong(int[] arr, int n)
    {
        int maxArmstrongNumber = 0;
 
        // Iterate through the array
        for (int i = 0; i < n; i++) {
            int number = arr[i];
            int sum = 0;
            int digits = 0;
            int temp = number;
 
            // Calculate the number of digits in the element
            while (temp != 0) {
                digits++;
                temp /= 10;
            }
 
            temp = number;
 
            // Calculate the sum of the digits raised to the
            // power of the number of digits
            while (temp != 0) {
                int digit = temp % 10;
                sum += Math.pow(digit, digits);
                temp /= 10;
            }
 
            // If the element is an Armstrong number and
            // greater than maxArmstrongNumber, update
            // maxArmstrongNumber
            if (sum == number
                && number > maxArmstrongNumber) {
                maxArmstrongNumber = number;
            }
        }
        // Return the largest Armstrong number in the array
        return maxArmstrongNumber;
    }
 
    public static void main(String[] args)
    {
        int[] arr = { 153, 9474, 1634, 371, 8208, 9475 };
 
        // Calculate the number of elements in the array
        int n = arr.length;
        System.out.println(
            "The largest Armstrong number in the array is: "
            + largestArmstrong(arr, n));
    }
}
 
// This code is contributed by Susobhan Akhuli


Python3




# Python program to find the largest armstrong number
# of the array
import math
 
def largest_armstrong(arr, n):
    maxArmstrongNumber = 0
 
    # Iterate through the array
    for i in range(n):
        number = arr[i]
        sum = 0
        digits = 0
        temp = number
 
        # Calculate the number of digits in the element
        while temp != 0:
            digits += 1
            temp //= 10
 
        temp = number
 
        # Calculate the sum of the digits raised to the
        # power of the number of digits
        while temp != 0:
            digit = temp % 10
            sum += math.pow(digit, digits)
            temp //= 10
 
        # If the element is an Armstrong number and greater
        # than maxArmstrongNumber, update
        # maxArmstrongNumber
        if sum == number and number > maxArmstrongNumber:
            maxArmstrongNumber = number
 
    # Return the largest Armstrong number in the array
    return maxArmstrongNumber
 
 
arr = [153, 9474, 1634, 371, 8208, 9475]
 
# Calculate the number of elements in the array
n = len(arr)
print("The largest Armstrong number in the array is:", largest_armstrong(arr, n))
 
# This code is contributed by Susobhan Akhuli


C#




// C# program to find the largest armstrong number
// of the array using System;
using System;
 
public class GFG
{
    public static int LargestArmstrong(int[] arr)
    {
        int maxArmstrongNumber = 0;
 
        // Iterate through the array
        for (int i = 0; i < arr.Length; i++)
        {
            int number = arr[i];
            int sum = 0;
            int digits = 0;
            int temp = number;
 
            // Calculate the number of digits in the element
            while (temp != 0)
            {
                digits++;
                temp /= 10;
            }
 
            temp = number;
 
            // Calculate the sum of the digits raised to the power of the number of digits
            while (temp != 0)
            {
                int digit = temp % 10;
                sum += (int)Math.Pow(digit, digits);
                temp /= 10;
            }
 
            // If the element is an Armstrong number and greater than maxArmstrongNumber, update maxArmstrongNumber
            if (sum == number && number > maxArmstrongNumber)
            {
                maxArmstrongNumber = number;
            }
        }
 
        // Return the largest Armstrong number in the array
        return maxArmstrongNumber;
    }
 
    public static void Main(string[] args)
    {
        int[] arr = { 153, 9474, 1634, 371, 8208, 9475 };
 
        // Calculate the number of elements in the array
        int n = arr.Length;
        Console.WriteLine("The largest Armstrong number in the array is: " + LargestArmstrong(arr));
    }
}


Javascript




// Javascript program to find the largest armstrong number
// of the array
function largestArmstrong(arr) {
    let maxArmstrongNumber = 0;
 
    // Iterate through the array
    for (let i = 0; i < arr.length; i++) {
        let number = arr[i];
        let sum = 0;
        let digits = 0;
        let temp = number;
 
        // Calculate the number of digits
        // in the element
        while (temp !== 0) {
            digits++;
            temp = Math.floor(temp / 10);
        }
 
        temp = number;
 
        // Calculate the sum of the digits
        // raised to the power of the
        // number of digits
        while (temp !== 0) {
            let digit = temp % 10;
            sum += Math.pow(digit, digits);
            temp = Math.floor(temp / 10);
        }
 
        // If the element is an Armstrong
        // number and greater than
        // maxArmstrongNumber, update
        // maxArmstrongNumber
        if (sum === number && number > maxArmstrongNumber) {
            maxArmstrongNumber = number;
        }
    }
 
    // Return the largest Armstrong
    // number in the array
    return maxArmstrongNumber;
}
 
// Driver code
const arr = [153, 9474, 1634, 371, 8208, 9475];
console.log("The largest Armstrong number in the array is: " + largestArmstrong(arr));
 
// This code is contributed by Susobhan Akhuli


Output

The largest Armstrong number in the array is: 9474




Time Complexity: O(N*(log(log M))), N for the iteration and log(log M) to calculate the sum of the digits raised to the power of the number of digits.
Auxiliary Space: O(1), as we are not using any extra space.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads