Question

In: Computer Science

static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or...

static int product(int x,int y){

if(x==0||y==0){//checking if x or y is 0

return 0;//if x or y is 0, then the return value and x*y will be zero.

}else if(y<0&&x<0){

x=-x;//Changing the sign of x

y=-y;//Changing the sign of y

}else if(x>=1){

return (y+product(x-1,y));

}

return (x+product(x,y-1));

}

find the space complexity and the time complexity of the above algorithm.

Solutions

Expert Solution

Time Complexity

Let the function T(x,y) denotes the number of operations perform by calling the product function:-

  • Base Case - When either of x or y is 0 , the function returns 0.Hence , it takes a fixed number of operations say 1.

    T(0,y) or T(y,0) = 1
  • Recursive Case:- If x or y is greater than 0, it will perform constant operations having constant cost say 1 and make a recursive call to T(x-1,y) if x>0 else T(x,y-1) .
    T(x,y) = 1+T(x-1,y) if x>0
    T(x,y) = 1+T(x,y-1) otherwise

    Case 1:- x>0
    T(x,y) = 1+T(x-1,y)
    1+T(x-1,y) = 2+ T(x-2,y)
    2+ T(x-2,y) = 3+ T(x-3,y)
    .
    .
    .
    .
    T(x,y) = k + T(x-k,y)
    if k = x
    T(x,y) = x+1                           {T(0,y) = 1}

    TIme Complexity = O(x)

    Case 2:- x<1
    Similarly , for this case

    Time Complexity = O(y)

---------------------------------------------------------------------------------

Space Complexity - The space complexity is determined by the depth of recursion tree i.e the number of return statements as every recursive call is stored in system stack.

  • Case 1:-x>0
    For this case, the number of recursive calls are x+1
    Space Complexity = O(x)
  • Case 2:-x<1
    For this case, the number of recursive calls are y+1
    Space Complexity = O(y)

Related Solutions

Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
Find y as a function of x if y′′′+25y′=0 y(0)=2,  y′(0)=20,  y′′(0)=−100 y(x)=
Find y as a function of x if y′′′+25y′=0 y(0)=2,  y′(0)=20,  y′′(0)=−100 y(x)=
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
y''(t)+(x+y)^2*y(t)=sin(x*t+y*t)-sin(x*t-y*t), y(0)=0, y'(0)=0, x and y are real numbers
y''(t)+(x+y)^2*y(t)=sin(x*t+y*t)-sin(x*t-y*t), y(0)=0, y'(0)=0, x and y are real numbers
Solve the IVP using Laplace transforms x' + y'=e^t -x''+3x' +y =0 x(0)=0, x'(0)=1, y(0)=0
Solve the IVP using Laplace transforms x' + y'=e^t -x''+3x' +y =0 x(0)=0, x'(0)=1, y(0)=0
Solve the following differential equations: 1.) y"(x)+ y(x)=4e^x ; y(0)=0, y'(0)=0 2.) x"(t)+3x'(t)+2x(t)=4t^2 ; x(0)=0, x'(0)=0
Solve the following differential equations: 1.) y"(x)+ y(x)=4e^x ; y(0)=0, y'(0)=0 2.) x"(t)+3x'(t)+2x(t)=4t^2 ; x(0)=0, x'(0)=0
a) y''(x)-3y'(x)=8e3x+4sinx b) y''(x)+y'(x)+y(x)=0 c) y(iv)(x)+2y''(x)+y(x)=0
a) y''(x)-3y'(x)=8e3x+4sinx b) y''(x)+y'(x)+y(x)=0 c) y(iv)(x)+2y''(x)+y(x)=0
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month...
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month and year entered as input parameter. Remember to invoke the isLeapYear method when appropriate. If the month entered as input parameter is 2 (February) and the year passed as input parameter is a leap year this method should return 29. private boolean isDateValid (int m, int d, int y) – return true if day, month, and year entered as input parameters form a valid...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT