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

Digit Sum of a Power of Two

Add up the decimal digits of 2 raised to the power n.

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

  • Double a number stored as digits
  • Sum the digits of a large number
  • Handle the n = 0 case

समस्या

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

Powers of two grow fast: 2^64 already has 20 digits and 2^2000 has over 600. Compute 2^n exactly and add up its decimal digits. Without big integers, keep the number as an array of digits and double it n times.

Implement digit_sum_power_of_two(n).

Input

n: The exponent.

Output

Return the sum of the decimal digits of 2^n.

सीमाएँ

0 ≤ n ≤ 2000

उदाहरण

उदाहरण 1
इनपुट:
n = 10

अपेक्षित आउटपुट:
expected = 7
इनपुट:
long long n = 10;

अपेक्षित आउटपुट:
long long expected = 7;
इनपुट:
long n = 10;

अपेक्षित आउटपुट:
long expected = 7;

व्याख्या

2^10 = 1024 and 1 + 0 + 2 + 4 = 7.

उदाहरण 2
इनपुट:
n = 0

अपेक्षित आउटपुट:
expected = 1
इनपुट:
long long n = 0;

अपेक्षित आउटपुट:
long long expected = 1;
इनपुट:
long n = 0;

अपेक्षित आउटपुट:
long expected = 1;

व्याख्या

2^0 = 1, so the digit sum is 1.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

2^n is 1 doubled n times.

संकेत 2

Store digits with the lowest digit first so carries move toward the end of the array.

संकेत 3

Add the digits only once the doubling is finished.

अपेक्षित जटिलता

O(n²) time, O(n) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

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

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

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

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

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