Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * As the function calls itself 'b' times (minus the base case),
- * the time complexity should be O(b). In a more generalized form,
- * if we equate b with the traditional complexity notation with N,
- * we can say the time complexity is O(N) where N = b.
- *
- * As for the space complexity, since it calls itself 'b' times (minus the base case), it
- * will add 'b' number of frames to the stack. Hence, the space complexity
- * will also be O(b). In a more generalized fashion, we can again write
- * the space complexity is O(N) where N = b.
- */
- int recursive_multiply (int a, int b) {
- if (a == 0 || b == 0) {
- return 0;
- }
- if (b == 1) {
- return a;
- }
- return a + recursive_multiply(a, b - 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement