Armstrong Number - Problem

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: 153 is a 3-digit number. 1³ + 5³ + 3³ = 1 + 125 + 27 = 153, so 153 is an Armstrong number.

Input & Output

Example 1 — Classic Armstrong Number
$ Input: n = 153
Output: true
💡 Note: 153 is a 3-digit number. 1³ + 5³ + 3³ = 1 + 125 + 27 = 153, which equals the original number.
Example 2 — Single Digit
$ Input: n = 9
Output: true
💡 Note: 9 is a 1-digit number. 9¹ = 9, which equals the original number.
Example 3 — Not Armstrong
$ Input: n = 123
Output: false
💡 Note: 123 is a 3-digit number. 1³ + 2³ + 3³ = 1 + 8 + 27 = 36 ≠ 123.

Constraints

  • 1 ≤ n ≤ 231 - 1

Visualization

Tap to expand
Armstrong Number - Visual Solution INPUT n = 153 Digits Breakdown: 1 5 3 Number of digits: k = 3 3-digit number Sum of cubes required ALGORITHM STEPS 1 Count Digits k = 3 (for 153) 2 Extract Each Digit Using mod and divide 3 Compute Powers Raise each to k-th power 1^3 = 1 x 1 x 1 = 1 5^3 = 5 x 5 x 5 = 125 3^3 = 3 x 3 x 3 = 27 Sum = 1 + 125 + 27 = 153 4 Compare sum == n ? true : false FINAL RESULT Verification: Sum of powers = 153 Original n = 153 153 == 153 [OK] Output: true 153 IS an Armstrong number! Other Armstrong Numbers: 0, 1, 2, ..., 9, 153, 370, 371, 407, 1634, ... Key Insight: An Armstrong number equals the sum of its digits each raised to the power of the total digit count. Time Complexity: O(d) where d = number of digits. Space Complexity: O(1) - only storing the sum. Formula: For k-digit number n, check if n == d1^k + d2^k + ... + dk^k TutorialsPoint - Armstrong Number | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
23.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen