Watch 8 video solutions for Armstrong Number, a easy level problem involving Math. This walkthrough by codedecks has 2,061 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 108Problem 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.
| 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 |