Skip to main content

Armstrong Number - Solution & Explanation

EasyPremiumFree on FleetCodeMath5 min readAsked at: Amazon
Practice this problem

Problem Statement

Given an integer n, return true if and only if it is an Armstrong number.

The k-digit number n is an Armstrong number if and only if the kth power of each digit sums to n.

 

Example 1:

Input: n = 153
Output: true
Explanation: 153 is a 3-digit number, and 153 = 13 + 53 + 33.

Example 2:

Input: n = 123
Output: false
Explanation: 123 is a 3-digit number, and 123 != 13 + 23 + 33 = 36.

 

Constraints:

  • 1 <= n <= 108

Approach Overview

Problem Overview: You are given an integer n. A number is called an Armstrong number if it equals the sum of its digits raised to the power of the number of digits. For example, 153 has three digits and satisfies 1^3 + 5^3 + 3^3 = 153. The task is to check whether the given number satisfies this property.

Approach 1: Digit Extraction Simulation (O(d) time, O(1) space)

This approach directly follows the definition of an Armstrong number. First determine the number of digits d in n. Then iterate through each digit by repeatedly taking n % 10 and dividing by 10. For every extracted digit, compute digit^d and add it to a running sum. After processing all digits, compare the computed sum with the original number.

The key insight is that digit extraction with modulo and integer division allows you to process digits without allocating additional memory. Every digit is visited exactly once, so the runtime is proportional to the number of digits in the number. This technique appears frequently in math and number theory problems where operations are performed digit by digit.

Because the algorithm stores only a few integer variables, the extra memory usage stays constant. This makes it the most efficient and interview-friendly solution.

Approach 2: String Conversion (O(d) time, O(d) space)

Another straightforward method converts the integer to a string and iterates over each character. The length of the string gives the digit count d. For every character, convert it back to an integer digit and compute digit^d, accumulating the result in a sum.

This approach is easier to read and implement in many languages because string iteration avoids manual modulo operations. However, it requires additional memory to store the string representation of the number. The time complexity remains linear in the number of digits, but the space complexity increases to O(d).

This pattern is common in beginner-friendly math problems where clarity matters more than micro-optimizations. Many developers prefer this method in scripting languages like Python or JavaScript.

Recommended for interviews: The digit extraction simulation is the preferred solution. It demonstrates comfort with numeric operations such as modulo and integer division, which interviewers often expect in math-focused problems. The string approach still works and clearly shows the logic, but the constant-space simulation signals stronger algorithmic fundamentals.

Solution

We can first calculate the number of digits k, then calculate the sum s of the kth power of each digit, and finally check whether s equals n.

The time complexity is O(log n), and the space complexity is O(log n). Here, n is the given number.

Code

Python

Java

C++

Go

TypeScript

JavaScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Digit Extraction SimulationO(d)O(1)Best general solution; minimal memory usage and common in math interview problems
String ConversionO(d)O(d)Useful when readability matters or when working in languages where string iteration is simpler

Video Solution

Armstrong number Leetcode 1134 | coding interview questions • codedecks • 2,154 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Armstrong Number easy or hard?
Armstrong Number is considered an easy problem. It focuses on basic math logic and digit manipulation rather than advanced algorithms or complex data structures.
Armstrong Number Python/Java solution
Both Python and Java implementations follow the same logic: compute the digit count, iterate through digits, calculate digit^d, and compare the total with the original number. The algorithm runs in O(d) time and uses constant extra space.
How to solve Armstrong Number in O(n)?
Treat n as the input number and iterate through its digits. First compute the digit count d, then extract each digit using n % 10 and accumulate digit^d. Since each digit is processed once, the complexity is linear in the number of digits.
What is the best approach for Armstrong Number?
The digit extraction simulation approach is the most efficient. Count the digits, then repeatedly extract each digit using modulo and compute digit^d. This runs in O(d) time with O(1) space, where d is the number of digits.
Is Armstrong Number asked at Google/Amazon/Meta?
Armstrong number checks commonly appear in screening rounds and basic coding assessments rather than advanced onsite interviews. They test understanding of digit manipulation, loops, and mathematical operations.
What data structure is used in Armstrong Number?
No special data structure is required. The solution typically uses basic integer variables and arithmetic operations such as modulo and division to iterate through digits.
What is the time complexity of Armstrong Number?
The time complexity is O(d), where d is the number of digits in the number. Each digit is processed exactly once to compute digit^d and add it to the sum.

Ready to solve this problem?

Practice Armstrong Number with our built-in code editor and test cases.

Practice on FleetCode