Check Armstrong Number
An easy Math problem included in Apna College. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Math
- Sheets
- 1
- Core for
- 13 roles
- Platform
- External
The problem
Given a positive integer n, determine if it is an Armstrong number (also known as a narcissistic number). An Armstrong number of k digits is equal to the sum of its own digits each raised to the power of k.
Example 1
- Input
- n = 153
- Output
- true
- Why
- 153 has 3 digits. 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153. So 153 is an Armstrong number.
Example 2
- Input
- n = 123
- Output
- false
- Why
- 123 has 3 digits. 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36, which is not equal to 123.
Example 3
- Input
- n = 9474
- Output
- true
- Why
- 9474 has 4 digits. 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474.
Constraints
- 1 <= n <= 10^8
How to think about it
Updated 2026-09-09The power k depends entirely on the total digit count, which must be determined before evaluating any powered terms. Because n <= 10^8, k is at most 8 and 9^8 fits comfortably in standard integer arithmetic, so early digit counting unlocks a single extraction pass.
Approaches, worst first
String conversion and power summing
time O(log10(n)) · space O(log10(n))
Convert n to a string to obtain its length k, then iterate through characters, converting each digit back to an integer and summing d^k. This incurs string allocation overhead.
Two-pass pure arithmeticWrite this one
time O(log10(n)) · space O(1)
Count digits using repeated integer division by 10 to find k, then copy n and extract each digit with modulo 10, adding digit^k to an accumulator. Check if accumulator equals n.
Where people lose marks · 3
- Assuming k is always 3; Armstrong numbers are defined for any digit length k, so calculating cubes for numbers like 9474 produces false negatives.
- Accumulator overflow if using 32-bit signed integers when summing powers for numbers with large k, though under n <= 10^8 the maximum possible sum is 8 × 9^8 = 344373768, which fits inside signed 32-bit int.
- Modifying n in place during the digit counting pass without saving a copy for the power summation pass.
The theory behind it
Math — the ground this problem stands on. All Math problems
What Math is
Algorithmic math is the application of number properties, modular arithmetic, and geometric patterns to compute results without simulating every step. Rather than filling memory buffers with millions of simulated counters or walking endless loops, mathematical formulation uses identities like prime factorization, digit extraction, and greatest common divisors to jump directly to target answers through closed-form rules.
When to reach for it
Reach for mathematical techniques when problem bounds are massive, such as constraints reaching ten to the eighteenth power where simulation is impossible. Strong prompts mention prime generation, greatest common divisor calculations, modular inverses, fast exponentiation, digit reversals, or combinatorial arrangements. If brute-force counting produces memory overflow or time limit exceeded warnings, algebraic reorganization is the intended path.
How the pattern works
Focus on numeric constraints and potential overflows before writing expressions. Break numbers into constituent digits using modulo ten to peel least significant figures and integer division to shrink scale. For divisibility, apply Euclid's remainder theorem repeatedly until remainder drops to zero. Apply modulo arithmetic at every intermediate addition and multiplication stage rather than once at the end, preventing values from spilling beyond standard numeric register limits.
What each operation costs
| Operation | Time |
|---|---|
| greatest common divisor via euclidean algorithm | O(log(min(a, b))) |
| modular exponentiation by repeated squaring | O(log n) |
| prime sieve of eratosthenes up to n | O(n log log n) |
What usually goes wrong with Math
- Allowing 32-bit signed integer overflow during intermediate multiplications before modulo reduction is applied, producing negative or truncated results.
- Failing to handle negative inputs in modulo arithmetic, producing negative remainders in languages that implement truncated rather than floored division.
- Dividing by zero when the denominator or remainder becomes zero at unexpected edge boundary values like empty sets or singletons.
Which roles need this problem
Math is a core topic for these 13 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 7 more roles, including Security Engineer, Performance Engineer, Site Reliability Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Math problems
Problem set and role mapping as of .