The following was a programming interview practice question.
What is the smart way to handle this?
A number M is stored in an array in the reverse order. For example, the number 274 is stored in the following array:
A[0]= 4 A[1]=7 A[2]=2
Write a function that given the array A representing some number, returns the sum of the digits of a decimal representation of the number M * 17. Array size can be very large (more than 2,000,000 elements).
Imagine you were multiplying 153 by 17 in longhand. It would look something like this:
153
17
---
51
85
17
----
2601
But you don't actually need to save the complete result; you only need to add the digits as you go along. So after the first step you know the last digit is 1, and you carry 5. Then after the second step you know the second digit is 0, and you carry 9. Then after the third step you know the third digit is 6, and you carry 2. When you run out of digits to multiply you just have to add the digits of the carry. (You can't carry more than 16, so you only have two cases to think about.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With