Advertisement
amitsen01ei

recusive_multiplication.cpp

Aug 20th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | Source Code | 0 0
  1. /*
  2.  * As the function calls itself 'b' times (minus the base case),
  3.  * the time complexity should be O(b). In a more generalized form,
  4.  * if we equate b with the traditional complexity notation with N,
  5.  * we can say the time complexity is O(N) where N = b.
  6.  *
  7.  * As for the space complexity, since it calls itself 'b' times (minus the base case), it
  8.  * will add 'b' number of frames to the stack. Hence, the space complexity
  9.  * will also be O(b). In a more generalized fashion, we can again write
  10.  * the space complexity is O(N) where N = b.
  11.  */
  12. int recursive_multiply (int a, int b) {
  13.  
  14.     if (a == 0 || b == 0) {
  15.         return 0;
  16.     }
  17.  
  18.     if (b == 1) {
  19.         return a;
  20.     }
  21.  
  22.     return a + recursive_multiply(a, b - 1);
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement