Question: 2. Write a recursive method that sums up all the odd numbers between 1 and n. As an example, if n == 5 the method

2. Write a recursive method that sums up all the odd numbers between 1 and n. As an example, if n == 5 the method must return 9, which is the sum 5 + 3 + 1. If n == 6 the method must return 9, as well, because the odd numbers to sum are still 5, 3, and 1. You must use the following method header. public static int recursiveOddSum(int n){

3. You have the following three methods.

public static void recursivePrint1(int r){

if (r==0)

return;

System.out.println(r);

recursivePrint1(r-1);

}

public static void recursivePrint2(int r){

if (r<=1)

return;

recursivePrint2(r/2);

System.out.println(r);

}

public static int recursivePrint3(int r){

System.out.println(r);

if (r<=1)

return 0;

int a= recursivePrint3(r/2);

int b= recursivePrint3(-r/2);

return a+b;

}

(a) What is the terminal output of the method call recursivePrint1(5)?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

(b) What is the terminal output of the method call recursivePrint2(5)?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

(c) What is the terminal output of the method call recursivePrint3(5)?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4. Suppose you have a doubly linked list of Node objects with char data elements. The Node class is provided below. class Node {

char data;

Node next; // links to the next node

Node prev; // links to the previous node

}

Consider that head is the reference variable that holds the starting non-null node and tail is the last non-null node of the doubly linked list you have. Obviously, both head and tail are instances of the Node class.

(a) Write a recursive method named isPalindrome that checks whether a sequence of characters in a doubly linked list of Node objects forms a palindrome. The method must return true if the sequence forms a palindrome; otherwise, it should return false. As examples, if the linked list contains {A, C, C, A} or {A, C, A} in a sequence then the isPalindrome method should return true. For a sequence, {A, C, C, B}, the isPalindrome method should return false. You must use the following header.

public static boolean isPalindrome(Node head, Node tail) {

(b) What is the time complexity (big-oh) of the isPalindrome method you have written?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5. Write a recursive method that will return the count of vowels in a string. For example, the method should return 5 for an input string application. Assume that string s will not contain uppercase letters.

public static int countVowels(String s){

6. Compute the big-oh time complexity of the following function. You must show the derivation for full credit.

T(n) = T(n/ 3) + c

You might need the following list of formulas to solve this problem.

1+2+3...+(n-1)+n = n(n+1)/2

a^0 + a^1+ a^2 + a^3+....+ a^(n-1)+a^n = a^(n+1)-1/a-1

2^0 + 2^1 + 2^2 + 2^3 + .....+ 2^(n-1) + 2^n = 2^(n+1)-1/2-1

7. Two algorithms, A and B, have the time functions f and g, where f(n) = 10n^2 + 16n + 11 and g(n) = 9n^2 + 22n + 6. Determine which algorithm runs faster. You must show the derivations for full credit. You will need to compute a maximum common point after which one algorithm is faster than the other but before that point the conclusion might not be true.

8. Show divide-and-conquer steps of merge sort on the following array: 15, 8, 13, 11, 7. You must show each step of the algorithm.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!