Question

In: Computer Science

The following pseudocode finds the maximum element in an array of size n. Int MAX (int...

The following pseudocode finds the maximum element in an array of size n.

Int MAX (int A [ ], int n) {

M = A[0];

for i = 1 to n – 1

    if (A[i] > M)

        M = A[i]        //Update the max

return M;

}

  1. Write a recursive version of this program.

  1. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n).

  1. Prove by induction that the solution of the recurrence is f(n) = n − 1.

Solutions

Expert Solution

Recursive version of this program:

Approach:

  1. We have to Recursively traverse the array from the end,
  2. Base case: If the remaining array is of length 1, then we have to return the only present element i.e. arr[0]
    if(n == 1)
       return arr[0];
  3. Recursive call: If the base case is not satisfied, then we will call the function by passing the array of one size less from the end, i.e. from arr[0] to arr[n-1].
  4. Return statement: At each recursive call (except for the base case), we have to return the maximum of the last element of the current array (i.e. arr[n-1]) and the element returned from the previous recursive call.
    return max(arr[n-1], recursive_function(arr, n-1));
    

Pseudocode:

Int MAX (int A [ ], int n) {
  if (n == 1) 
      return A[0]
  
  if(A[n-1] > MAX(A, n-1))
      return A[n-1]
  else
      return MAX(A, n-1)
}

Now, let f(n) be the number of key comparisons performed by this algorithm, then from the above recursive algorithm, f(n) = f(n - 1) + 1

Here, 1 denotes 1 key comparisons happening in base case.

Now, solving f(n), given f(n) = 1 (base case)

f(n) = f(n-1) + 1

f(n-1) = f(n-2) + 1

...

...

f(2) = f(1) + 1

Here, adding RHS and LHS, f(n) = f(1) + n - 2 = n - 1

Therefore f(n) = n - 1

Now, proving this using Induction:

Base Case: f(n) = 1

Assume for n = k, this function is satisfied: f(k) = k - 1

Now, Proving it for n = k + 1

f(k + 1) = k + 1 - 1 = k

So, true for n = k + 1

Hence, the expression is true,                                             PROVED


Related Solutions

// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++)...
// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++) { System.out.println(n); } System.out.println(); // 2. System.out.println("2."); int total = 25; for (int number = 1; number <= (total / 2); number++) { total = total - number; System.out.println(total + " " + number); } System.out.println(); // 3. System.out.println("3."); for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1;...
Element Shifter: Write a function that accepts an int array and the array’s size as arguments....
Element Shifter: Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The...
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ).
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ). Given a sorted array of distinct integers and a single integer value, find all pairs of indexes in the list whose product is equal to the value. Return the index pairs on the same row of a two-dimensional array. Note, m will be the number of rows and total number of pairs found, while 2 will be the number of columns along every row...
//2.-------------------------------------------------------------- Given an array of size N, what is the complexity? int m = A[0]; for...
//2.-------------------------------------------------------------- Given an array of size N, what is the complexity? int m = A[0]; for (int i=1; i<N; i++) { if (A[i] > m) m = A[i]; } int k = 0; for (int i=0; i<N; i++) { if (A[i] == m) k++; } What is a (name one) most frequent operation performed?______________________ Expression showing the number of times it is performed ___________ Time Complexity in Big-O Notation O( ) _____________________________________________________ //3.-------------------------------------------------------------- int mult(int N, int M) {   ...
The following is the pseudocode of algorithm that searches a sorted array of n items by...
The following is the pseudocode of algorithm that searches a sorted array of n items by dividing it into three sub arrays of almost n/3 items (7pt). Hint: refer Mergesort algorithm) Input: Sorted array of Keys(A) indexed from 1 to n. Output: Location, the Location of K in array A (hint: return 0 if K is no in A) index location3 (int A[1…n ], index L, index H) { index m1, m2; if (L > H) return 0; else   ...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
This code assigns the maximum of the values 3 and 5 to the int variable max...
This code assigns the maximum of the values 3 and 5 to the int variable max and outputs the result int max; // your code goes here This code prompts the user for a single character and prints "true" if the character is a letter and "false" if it is not a letter // your code goes here
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT