Find largest Armstrong number in an Array
Last Updated :
11 Jul, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int largest_Armstrong( int arr[], int n)
{
int maxArmstrongNumber = 0;
for ( int i = 0; i < n; i++) {
int number = arr[i];
int sum = 0;
int digits = 0;
int temp = number;
while (temp != 0) {
digits++;
temp /= 10;
}
temp = number;
while (temp != 0) {
int digit = temp % 10;
sum += pow (digit, digits);
temp /= 10;
}
if (sum == number && number > maxArmstrongNumber) {
maxArmstrongNumber = number;
}
}
return maxArmstrongNumber;
}
int main()
{
int arr[] = { 153, 9474, 1634, 371, 8208, 9475 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "The largest Armstrong number in the array is: "
<< largest_Armstrong(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int largestArmstrong( int [] arr, int n)
{
int maxArmstrongNumber = 0 ;
for ( int i = 0 ; i < n; i++) {
int number = arr[i];
int sum = 0 ;
int digits = 0 ;
int temp = number;
while (temp != 0 ) {
digits++;
temp /= 10 ;
}
temp = number;
while (temp != 0 ) {
int digit = temp % 10 ;
sum += Math.pow(digit, digits);
temp /= 10 ;
}
if (sum == number
&& number > maxArmstrongNumber) {
maxArmstrongNumber = number;
}
}
return maxArmstrongNumber;
}
public static void main(String[] args)
{
int [] arr = { 153 , 9474 , 1634 , 371 , 8208 , 9475 };
int n = arr.length;
System.out.println(
"The largest Armstrong number in the array is: "
+ largestArmstrong(arr, n));
}
}
|
Python3
import math
def largest_armstrong(arr, n):
maxArmstrongNumber = 0
for i in range (n):
number = arr[i]
sum = 0
digits = 0
temp = number
while temp ! = 0 :
digits + = 1
temp / / = 10
temp = number
while temp ! = 0 :
digit = temp % 10
sum + = math. pow (digit, digits)
temp / / = 10
if sum = = number and number > maxArmstrongNumber:
maxArmstrongNumber = number
return maxArmstrongNumber
arr = [ 153 , 9474 , 1634 , 371 , 8208 , 9475 ]
n = len (arr)
print ( "The largest Armstrong number in the array is:" , largest_armstrong(arr, n))
|
C#
using System;
public class GFG
{
public static int LargestArmstrong( int [] arr)
{
int maxArmstrongNumber = 0;
for ( int i = 0; i < arr.Length; i++)
{
int number = arr[i];
int sum = 0;
int digits = 0;
int temp = number;
while (temp != 0)
{
digits++;
temp /= 10;
}
temp = number;
while (temp != 0)
{
int digit = temp % 10;
sum += ( int )Math.Pow(digit, digits);
temp /= 10;
}
if (sum == number && number > maxArmstrongNumber)
{
maxArmstrongNumber = number;
}
}
return maxArmstrongNumber;
}
public static void Main( string [] args)
{
int [] arr = { 153, 9474, 1634, 371, 8208, 9475 };
int n = arr.Length;
Console.WriteLine( "The largest Armstrong number in the array is: " + LargestArmstrong(arr));
}
}
|
Javascript
function largestArmstrong(arr) {
let maxArmstrongNumber = 0;
for (let i = 0; i < arr.length; i++) {
let number = arr[i];
let sum = 0;
let digits = 0;
let temp = number;
while (temp !== 0) {
digits++;
temp = Math.floor(temp / 10);
}
temp = number;
while (temp !== 0) {
let digit = temp % 10;
sum += Math.pow(digit, digits);
temp = Math.floor(temp / 10);
}
if (sum === number && number > maxArmstrongNumber) {
maxArmstrongNumber = number;
}
}
return maxArmstrongNumber;
}
const arr = [153, 9474, 1634, 371, 8208, 9475];
console.log( "The largest Armstrong number in the array is: " + largestArmstrong(arr));
|
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.
Share your thoughts in the comments
Please Login to comment...