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

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...
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...
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...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT