214. Multiply Without Multiplying
Foundation1000 ms256 MBSolved by 0%
Print a multiplied by b, without using the multiplication operator.
Recursively, a x b is a + (a x (b - 1)), and a x 0 is 0.
Read the constraint on b before you submit. Adding a to itself a billion
times will not finish, and the way out is the same trick that makes fast
exponentiation work: halve b instead of decreasing it by one.
Constraints - `0 ≤ a ≤ 1000000` - `0 ≤ b ≤ 1000000000`
Input
A single line containing two integers a and b, separated by a space.
Output
Print one integer, the product of a and b.
Input6 7
Output42
Noteis a small product
Input5 0
Output0
Noteis the base case, where the answer is 0
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.