14(June) Armstrong Numbers

14. Armstrong Numbers

The problem can be found at the following link: Question Link

Problem Description

You are given a 3-digit number n. Determine whether it is an Armstrong number or not.

An Armstrong number of three digits is a number such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since (3^3 + 7^3 + 1^3 = 371).

Example:

Input:

n = 153

Output:

Yes

Explanation: 153 is an Armstrong number since (1^3 + 5^3 + 3^3 = 153). Hence, the answer is "Yes".

My Approach

  1. Initialization:

    • Convert the number to a string to easily access each digit.

  2. Sum Calculation:

    • Calculate the sum of the cubes of each digit.

  3. Comparison:

    • Compare the calculated sum with the original number.

    • If they are equal, return "Yes".

    • Otherwise, return "No".

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(1), as we perform a constant number of operations regardless of the size of the input.

  • Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space.

Code

C++

Java

Python

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated