Question: during the class, the following reduce-and-conquer algorithm was introduced to find the sum of all elements in the array. With each iteration, the algorithm reduces

during the class, the following reduce-and-conquer algorithm was introduced to find the sum of all elements in the array. With each iteration, the algorithm reduces the number of elements in array by one, and call the function recursively. Modify the algorithm so that, with each iteration, the algorithm reduces the number of elements by 2 elements, provided there are more than 2 elements in the array. To compute the complexity of your algorithm, find the recurrence relationship for the algorithm (using worst case scenario) and solve it using forward or backward substitution.

Please use the worst case scenario I want to know if the anser is T(n) = 2n -1 or 2n+1

Algorithm LinearRecSum(A, i, n):

Input: An array A, an int i (starting index for the array) and n (# of elements)

Output: The sum of the n integers in A starting at index i

if n = 1 then

return A[i ]

else

return A[i]+ LinearRecSum(A, i+1, n-1)

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!