आसान 0 हल किए गए 0% स्वीकृति

Spy Number

Check whether the sum of a number's digits equals the product of its digits.

आप क्या सीखेंगे

  • Track a sum and a product in one pass
  • Start a product at 1 and a sum at 0
  • Notice how a 0 digit affects the product

समस्या

अनुवाद दिखाएँ

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

उदाहरण

उदाहरण 1
इनपुट:
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.

उदाहरण 2
इनपुट:
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।