Problema
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.
Restrições
0 ≤ n ≤ 2000
Exemplos
Entrada:
n = 10
Saída esperada:
expected = 7
Entrada:
long long n = 10;
Saída esperada:
long long expected = 7;
Entrada:
long n = 10;
Saída esperada:
long expected = 7;
Explicação
2^10 = 1024 and 1 + 0 + 2 + 4 = 7.
Entrada:
n = 0
Saída esperada:
expected = 1
Entrada:
long long n = 0;
Saída esperada:
long long expected = 1;
Entrada:
long n = 0;
Saída esperada:
long expected = 1;
Explicação
2^0 = 1, so the digit sum is 1.
Como abordar
Tente sozinho primeiro e depois abra uma dica de cada vez.
Dica 1
2^n is 1 doubled n times.
Dica 2
Store digits with the lowest digit first so carries move toward the end of the array.
Dica 3
Add the digits only once the doubling is finished.
Complexidade esperada
O(n²) time, O(n) extra space
Referências
Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.