Armstrong Number - Solution & Explanation
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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Digit Extraction Simulation | O(d) | O(1) | Best general solution; minimal memory usage and common in math interview problems |
| String Conversion | O(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 Python/Java solution
How to solve Armstrong Number in O(n)?
What is the best approach for Armstrong Number?
Is Armstrong Number asked at Google/Amazon/Meta?
What data structure is used in Armstrong Number?
What is the time complexity of Armstrong Number?
Ready to solve this problem?
Practice Armstrong Number with our built-in code editor and test cases.
Practice on FleetCode