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