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)) ;
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...
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)=
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
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...
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
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';   ...
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';        }        String temp="";        for (int i = 0; i < str.length(); i++) {                 if(!temp.contains(String.valueOf(str.charAt(i)))) {                     temp += String.valueOf(str.charAt(i));                 }             }        char[] tempArray=stringToArray(temp);        int[] countArr=new int[tempArray.length];        int max=0;        for(int i=0;i<tempArray.length;i++) {            int cnt=numOccurences(tempArray[i],str);            countArr[i]=cnt;...
f(x,y)=3(x+y) 0<x+y<1, 0<x<1, 0<y<1 (a) E(xy|x)=? (b) Cov(x,y)=? (c) x and y is independent? thank you!
f(x,y)=3(x+y) 0<x+y<1, 0<x<1, 0<y<1 (a) E(xy|x)=? (b) Cov(x,y)=? (c) x and y is independent? thank you!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT