Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1134. Armstrong Number - Leetcode Solution

Problem Description

The Armstrong Number problem asks you to determine if a given integer n is an Armstrong number. An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.

  • For example, 153 is an Armstrong number because 13 + 53 + 33 = 153.
  • For a single-digit number, like 7, it is always an Armstrong number because 71 = 7.

Constraints:

  • The input is a non-negative integer n.
  • You must return true if n is an Armstrong number, and false otherwise.
  • There is only one valid answer for each input.

Thought Process

To solve this problem, you need to compare the original number to the sum of its digits each raised to the power of the number of digits. The most straightforward way is to:

  • Extract each digit of the number.
  • Count how many digits there are.
  • Raise each digit to the power of the number of digits and sum them up.
  • Compare the sum to the original number.

A brute-force approach would be to convert the number to a string, iterate over each character, convert it back to an integer, and perform the calculation. This is both easy to implement and efficient for typical input sizes.

There aren't significant optimizations needed because the number of digits in n is small (at most 10 for 32-bit integers), so iterating over the digits is fast.

Solution Approach

Let's break down the steps to check if a number is an Armstrong number:

  1. Convert the number to its digits:
    • You can do this by converting the number to a string and iterating over each character, or by repeatedly dividing by 10 and taking the remainder.
  2. Count the number of digits:
    • If you use a string, the length of the string is the number of digits.
    • If you use division, count how many times you divide before reaching zero.
  3. Sum the powers:
    • For each digit, raise it to the power of the number of digits and add to a running sum.
  4. Compare the sum to the original number:
    • If they're equal, return true; otherwise, return false.

This approach is both readable and efficient for all practical input sizes, and it works in any programming language.

Example Walkthrough

Let's walk through the process with an example input: n = 9474.

  1. Convert to digits:
    • The digits are 9, 4, 7, and 4.
  2. Count the digits:
    • There are 4 digits.
  3. Sum of powers:
    • 94 = 6561
    • 44 = 256
    • 74 = 2401
    • 44 = 256
    • Sum = 6561 + 256 + 2401 + 256 = 9474
  4. Comparison:
    • The sum (9474) equals the original number (9474), so 9474 is an Armstrong number.

Time and Space Complexity

Brute-force approach:

  • Time complexity: O(d), where d is the number of digits in n (since we process each digit once).
  • Space complexity: O(1) if we process digits numerically; O(d) if we convert to a string, but for small d this is negligible.

Since the number of digits in any integer is at most 10 (for 32-bit numbers), this is extremely efficient.

Optimized approach:
  • There is no significant optimization over the brute-force approach for this problem, as the operation is already efficient.

Summary

The Armstrong Number problem is a classic example of digit manipulation. The solution is elegant because it leverages simple arithmetic and string processing to check a property of numbers. By breaking the number into digits, raising each to the appropriate power, and summing, we can efficiently determine if a number is an Armstrong number. The approach is both easy to implement and efficient for any reasonable input.

Code Implementation

class Solution:
    def isArmstrong(self, n: int) -> bool:
        digits = [int(d) for d in str(n)]
        k = len(digits)
        return sum(d ** k for d in digits) == n
      
class Solution {
public:
    bool isArmstrong(int n) {
        int num = n, k = 0;
        while (num) {
            k++;
            num /= 10;
        }
        num = n;
        int sum = 0;
        while (num) {
            int digit = num % 10;
            int powDigit = 1;
            for (int i = 0; i < k; ++i) powDigit *= digit;
            sum += powDigit;
            num /= 10;
        }
        return sum == n;
    }
};
      
class Solution {
    public boolean isArmstrong(int n) {
        int num = n, k = 0;
        while (num > 0) {
            k++;
            num /= 10;
        }
        num = n;
        int sum = 0;
        while (num > 0) {
            int digit = num % 10;
            int powDigit = 1;
            for (int i = 0; i < k; ++i) powDigit *= digit;
            sum += powDigit;
            num /= 10;
        }
        return sum == n;
    }
}
      
var isArmstrong = function(n) {
    const digits = n.toString().split('').map(Number);
    const k = digits.length;
    let sum = 0;
    for (let d of digits) {
        sum += Math.pow(d, k);
    }
    return sum === n;
};