समस्या
A number with k digits is an Armstrong number if the sum of each digit raised to the power k equals the number. For example 153 = 1³ + 5³ + 3³.
Implement is_armstrong(n).
Input
n: A non-negative integer.
Output
Return true if n is an Armstrong number.
सीमाएँ
0 ≤ n ≤ 10¹⁸
उदाहरण
इनपुट:
n = 153
अपेक्षित आउटपुट:
expected = True
इनपुट:
long long n = 153;
अपेक्षित आउटपुट:
bool expected = true;
इनपुट:
long n = 153;
अपेक्षित आउटपुट:
bool expected = true;
व्याख्या
1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
इनपुट:
n = 10
अपेक्षित आउटपुट:
expected = False
इनपुट:
long long n = 10;
अपेक्षित आउटपुट:
bool expected = false;
इनपुट:
long n = 10;
अपेक्षित आउटपुट:
bool expected = false;
व्याख्या
1² + 0² = 1, which is not 10.
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Count how many digits the number has before summing powers.
संकेत 2
Compare the original number with the sum of each digit raised to that digit count.
संकेत 3
Remember that 0 is a special one-digit case.
अपेक्षित जटिलता
O(k) time, O(1) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।