Question

Find the time complexity of the following code: i am confused because addA calls addB and...

Find the time complexity of the following code: i am confused because addA calls addB and so does addB.

   void addA (int n) {
       if (n == 0) {
           System.out.println("hi");
       } else {
           addA( n - 1);
           addB( n - 1);

       }
   }

   void addB(int n) {

       if (n == 0) {
           System.out.println("hello");
       } else {
           addA( n - 1);
           addB(n - 1);
       }

   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to above program.

the recurrence relation for addA function is

addA(n) = addA(n-1) + addB(n-1)

the recurrence relation for addB is

addB(n) = addA(n-1) + addB(n-1)

since both are same we can denote them with single recurrence relation

T(n) =2T(n-1)

T(n) = 2 ( 2(T(n-2))

T(n) = 23(T(n-3))

..

.

.

T(n) = 2nT(0))

T(0) = 1 since it is the base case

now since each time we are reducing the value of n by one hence there will be total n times addA(n) will be there total time complexity of program is O(2n)

Add a comment
Know the answer?
Add Answer to:
Find the time complexity of the following code: i am confused because addA calls addB and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the time complexity of the following code segment? for (int i = 0; i<n;...

    What is the time complexity of the following code segment? for (int i = 0; i<n; i--) if (a[i] != 0) sum = a[i]; What is the time complexity of the following code segment? for (int i = 0; i<10; i++) if (a[i] != 0) sum += a[i]; What is the time complexity of the following code segment? for (int i = 0; i<n/2; i++) if (a[i] != 0) sum += a[i]; What is the time complexity of the following...

  • find complexity Problem 1 Find out the computational complexity (Big-Oh notation) of the code snippet: Code...

    find complexity Problem 1 Find out the computational complexity (Big-Oh notation) of the code snippet: Code 1: for (int i = n; i > 0; i /= 2) { for (int j = 1; j < n; j *= 2) {     for (int k = 0; k < n; k += 2) {               // constant number of operations here     } } } Code 2: Hint: Lecture Note 5, Page 7-8 void f(int n) { if (n...

  • What is the RUN TIME COMPLEXITY of the following code: ................................................................................. public void printLevelsRecursively(Node root) {...

    What is the RUN TIME COMPLEXITY of the following code: ................................................................................. public void printLevelsRecursively(Node root) { for (int i = 1; i <= heightOfTree(root); i++) { System.out.print("Level " + i + " : "); printSingleLevelRecursively(root, i); System.out.print("\n"); } } public int heightOfTree(Node root) { if (root != null)    return super.max(heightOfTree(root.l), heightOfTree(root.r)) + 1; return 0; } public void printSingleLevelRecursively(Node root, int level) { if (root == null) return; if (level == 1) System.out.print(root.key + " "); else if (level...

  • What is the time complexity (Big-O) of the following code? class Main {    // Recursive...

    What is the time complexity (Big-O) of the following code? class Main {    // Recursive function to generate all permutations of a String    private static void permutations(String candidate, String remaining)    {        if (remaining.length() == 0) {            System.out.println(candidate);        }        for (int i = 0; i < remaining.length(); i++)        {            String newCandidate = candidate + remaining.charAt(i);            String newRemaining = remaining.substring(0, i) +...

  • I am confused about how to code this. I have tried multiple ways of coding this...

    I am confused about how to code this. I have tried multiple ways of coding this and nothing has worked for me. If someone could help that would be great thanks. /*Chess Board: You will also create 5 "lanes" of "pieces" and each lane has that number of pieces on it; i.e. lane 5 = 5 pieces, lane 4 = 4 pieces, etc. The user and the computer can take any number of pieces from one lane. The object is...

  • Using C++ please explain What is the Big-O time complexity of the following code: for (int...

    Using C++ please explain What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...

  • 1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ)....

    1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • (C++/Linux) I am a little confused as to what the first line in the main exactly...

    (C++/Linux) I am a little confused as to what the first line in the main exactly does. I am also in need of clarity about all the possibilities in the if statement and what they mean and correspond to (all in the code below). Thank you. #include <iostream> #include <unistd.h> int main() { pid_t id = fork(); if(id == -1) { std::cout << "Error creating process\n"; } else if (id == 0) { std::cout << "I am a child process!\n";...

  • Question 1 (25 pts) Find the running time complexity for the following code fragments. Express yo...

    Question 1 (25 pts) Find the running time complexity for the following code fragments. Express your answers using either the Big-O or Big-Θ notations, and the tightest bound possible. Justify your answers. for(int count O , i -0; i < n* n; i++) for(int i0 ; j <i; j++) count++ for(int count O , i -0; i

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT