Question

Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This algorithm is based on the fWe can use a greedy algorithm to decide how far to the left to include: starting at the middle, move backward one index at a

Convert the pseudocode into a C++ function

Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This algorithm is based on the following ideas: In the base case, n 1 and the only possible solution is b 0, e 1 In the general case, divide V into a left and rnight half; then the maximum subarray can be in one of three places: o entirely in the left half; o entirely in the right half; or o starting in the left half and ending in the night half (cxossing the midpoint) The "entirely in2 cases can be handled with recursion; we can use a helper sub-algorithm to handle the "crossing" case. The overall optimal solution is the best of these three solutions. . maximum subarray dbh (V) return maximum subarray_recurse (V, 0, n) maximum subarray recurse (V, low, high): if low -high: return (low, low+1) middlelow+high) / 2 entirely left = maximum subarray recurse (v, 1 w, miadle) entirely right - maximum subarray recurse (Vv, middle + 1, high) crossing maximum subarray_crossing (V, low, middle, high) return (whichever of entirely left, entirely right, and crossina have the greatest sum) The sub-algorithm to find the maximum crossing subarray is based on these ideas: By definition we know that this subarray must include at least one element in the left half, and at least one element in the right half »
We can use a greedy algorithm to decide how far to the left to include: starting at the middle, move backward one index at a time, keeping track of the sum of all the elements we've visited. The best index to start with is whichever one has the largest sum. Likewise, we can use a greedy algonthm to decide how far to the right to include: starting at the middle, move forward one index at a time, keeping track of sums, and choosing whichever element maximizes the sum on the right side. maximum subarray crossing (V, low, middle, high: left-sum = right-sum = negative infinity f r i from middle down t 1 w : sum += v[i] if sum > left_sum: left sum sum sum O f r i from middle + 1 t high: umV[i] if sum > right sum: right sum surm return (b, e) This algorithm uses the idea of negative infinity, which exists in pseudocode, but not in the C++ programming language. Devising a way to implement this part of the algonthm is part of the project. You will probably need to wite helper functions to implement this algorithm; this is expected and totally fine. In the maximum subarray_crossing algorithm, each of the for loops takes O(n) time, and the rest of the algorithm takes 1) time, so the crossing sub-algoithm takes O(n) time. The time complexity of the entire maximumsubarray_recurse algorithm corresponds to a recurrence like T(n) = 2T(n/2)-O(n) so, bv the master theorem, and like merge sort, the time complexity ofmaximum subarray exh is O(n logn).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include <iostream>
#include <limits.h>
using namespace std;  
   
int max(int a, int b) { return (a > b)? a : b; }
int maxCrossingSum(int arr[], int l, int m, int h)
{
int sum = 0;
int left_sum = INT_MIN;
for (int i = m; i >= l; i--)
{
sum = sum + arr[i];
if (sum > left_sum)
left_sum = sum;
}
sum = 0;
int right_sum = INT_MIN;
for (int i = m+1; i <= h; i++)
{
sum = sum + arr[i];
if (sum > right_sum)
right_sum = sum;
}
  
return left_sum + right_sum;
}
  
int maxSubArraySum(int arr[], int l, int h)
{
if (l == h)
return arr[l];
  
int m = (l + h)/2;
  
return max(maxSubArraySum(arr, l, m), max(maxSubArraySum(arr, m+1, h),
maxCrossingSum(arr, l, m, h)));
}

int main()
{
int arr[] = {-2, -5, 6, -2, -3, 1, 5, -6};
int n = sizeof(arr)/sizeof(arr[0]);
  
cout<<"Maximum contiguous sum is : "<<maxSubArraySum(arr, 0, n-1);

return 0;
}
//sample output

C:\Users\IshuManish\Documents\ maxsubarraysum.exe Maximum contiguous sum is ? Process exited after 0.06511 seconds with retur

Add a comment
Know the answer?
Add Answer to:
Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This...
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
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