Question

What are the valid runtime bounds for the method mystery with respect to argument n? Select...

What are the valid runtime bounds for the method mystery with respect to argument n? Select all that apply.

public static int mystery(int n) {
    for (int i = 1; i < n; i = i + i) {
        for (int j = 0; j < i; j++) {
            for (int k = 0; k < n; k++) {
                f0(n);
            }
        }
    }
    return -1;
}
public static void f0(int n) {
    int[] arr = new int[200];
    for (int i = 0; i < 200; i++) {
        arr[i] = i;
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Explanation:

mystery function:

=>We can see in the function mystery(int n) there are three loops.

=>Outer for loop is independent loop and will run logn times from i = 1 to i < n doubling the value of i each time.

=>Middle for loop is dependent for loop which is depending upon outer for loop and will run from j = 0 to j < i incrementing the value of j by 1 each time.

=>Inner for loop is independent for loop and will run n times from k = 0 to k = n-1 incrementing the value of k by 1 each time.

=>After all loops f0(n) functions is called.

fo(n) function:

=>In f0 function there is only one for loop which is running 200 times from i = 0 to i = 199 incrementing the value of i by 1 each time.

=>Hence total number of times loop will be executed in f0 functions = 200 times.

=>Time complexity of f0 function = O(1)

Calculating overall time complexity:

=>Two for loops in mystery() function will be executed like: if i = 1 then middle loop will run only once, if i = 2 then middle loop will run twice, if i = 4 then middle loop will run 4 time and if i = 2^k where k is some constant then middle loop will run 2^k times.

=>Total number of time outer and middle loops will be executed = 1+ 2 + 4 + 8 + ... + 2^k where 2^k < n

=>Total number of time outer and middle loops will be executed = 1*(2^(k+1) - 1)

=>Total number of time outer and middle loops will be executed = 2*2^k - 1...(1)

=Let say 2^k = n (nearly equal)

=>k = log2(n)...(1)

From (1) and (2)

=>Total number of time outer and middle loops will be executed = 2*2^log2(n) - 1

=>Total number of time outer and middle loops will be executed = 2n - 1

=>Inner loop will run n times whenever it is executed so total number of times all loops will be executed = (2n-1)*n

=>Total number of times including f0 = (2n-1)*n*200

=>Time complexity = O(n^2)

I have explained each and every part with the help of statements attached to it.

Add a comment
Know the answer?
Add Answer to:
What are the valid runtime bounds for the method mystery with respect to argument n? Select...
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
  • Select all the valid asymptotic runtime bounds for the following function f2 in the worst case:...

    Select all the valid asymptotic runtime bounds for the following function f2 in the worst case: public static int f1 (int n) { int x = 0; for (int i = 0; i < n; i++) { x++; } return x; } public static int f2 (int n) { if (n <= 1) { return 1; } return f1(n) + f2(n/2) + f2(n/2); } Θ(n^2) O(n^2) Θ(log(n)) Θ(log^2(n)) Θ(nlog(n)) Ω(n) Ω(n^2)

  • Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int...

    Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...

  • What is the runtime of each method? Give answer in Θ(big Theta) notation as a function...

    What is the runtime of each method? Give answer in Θ(big Theta) notation as a function of n, give a brief explanation. A. public static int method1(int n){ int mid = n/2; for (int i = mid; i >= 0; i--) System.out.println(i); for (int i = mid + 1; i <= n; i++) System.out.println(i); return mid; } B. public static int method2(int n){ for (int i = n; i >= 0; i / 3){ System.out.println(i ); } return mid; }...

  • I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need...

    I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...

  • 5. What is the Big Oh method m2? public static void m2(int[] arr, int n) for...

    5. What is the Big Oh method m2? public static void m2(int[] arr, int n) for (int í = 1; í <= n- 1; i++) pM2(arr [i], arr, 0, i - 1); // end m2 private static void pM2(int entry, int[l arr, int begin, int end) int i- end; for(; (i 〉= begin) && (entry 〈 arr [i]); i--) arr [1 + 1] = arr L1] arr[i + 1] - entry; return // end pM2

  • pls help, idk whats wrong with this Add the reverse() method which reverses the content of...

    pls help, idk whats wrong with this Add the reverse() method which reverses the content of array without using additional array and the rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • 3. Consider the mystery method given. public static int mystery ( int n) [ if (n...

    3. Consider the mystery method given. public static int mystery ( int n) [ if (n == 0 ) { return 1; How do we get the values for recurse? else if (n%2 == 0 ) { int recurse = mystery ( n - 1); int result = recurse + n; return result; since n =5, we go to the else statement and do int recurse = mystery(5-1) which equals 4? why is 3 written? else { int recurse =...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • a. Draw a memory diagram to illustrate the passing of parameters with respect to the method...

    a. Draw a memory diagram to illustrate the passing of parameters with respect to the method call r.compareTo(s) in the following code. More specifically, illustrate how the calling object and parameter object are referenced during the execution of the method r.compareTo(s). b. For the same definition for class Rectangle as part a., draw the memory diagram that illustrates the storage of array and objects when the following code executes: class Rectangle { private double length, breadth; public Rectangle(double 1, double...

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
ADVERTISEMENT