समस्या
A spy number is a positive integer whose digit sum equals its digit product. For example, 1124 has sum 1 + 1 + 2 + 4 = 8 and product 1 × 1 × 2 × 4 = 8, so it is a spy number.
Implement spy_number(n).
Input
n: The number to test.
Output
Return true if the sum of the decimal digits of n equals their product, otherwise false. Every single-digit number is a spy number.
सीमाएँ
1 ≤ n ≤ 10^18
उदाहरण
इनपुट:
n = 123
अपेक्षित आउटपुट:
expected = True
इनपुट:
long long n = 123;
अपेक्षित आउटपुट:
bool expected = true;
इनपुट:
long n = 123;
अपेक्षित आउटपुट:
bool expected = true;
व्याख्या
Sum 1 + 2 + 3 = 6 and product 1 × 2 × 3 = 6 are equal, so the answer is true.
इनपुट:
n = 1234
अपेक्षित आउटपुट:
expected = False
इनपुट:
long long n = 1234;
अपेक्षित आउटपुट:
bool expected = false;
इनपुट:
long n = 1234;
अपेक्षित आउटपुट:
bool expected = false;
व्याख्या
Sum 1 + 2 + 3 + 4 = 10 but product 1 × 2 × 3 × 4 = 24, so the answer is false.
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Walk over the digits once and update both totals.
संकेत 2
Start the product at 1, not 0.
संकेत 3
Any number containing a 0 digit has product 0, so it cannot be spy unless the sum is 0 too.
अपेक्षित जटिलता
O(d) time for d digits, O(1) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।