Question

In class we discussed an approach that yields a Θ(n) algorithm for solving the maximum subarray...

In class we discussed an approach that yields a Θ(n) algorithm for solving the maximum subarray problem. This approach incrementally calculates the maximum subarray for A[1..j] that uses A[j], and then uses that value to calculate the maximum subarray for A[1..j+1] that uses A[j+1]. The answer is the maximum of all those n calculated values. Write pseudocode for a version of this algorithm that instead works from the right side of the array to the left. That is, it calculates the maximum subarray for A[j..n] that uses A[j], and then use that result to calculate the maximum subarray for A[j-1..n] that uses A[j-1]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pseudocode for the above problem is as follows-

Initialize: max_so_far = 0 max_ending_here = 0 Loop for each element of the array from right to left (a) max_ending_here = max_ending_here + a[i] (b) if(max_ending_here < 0) max_ending_here = 0 (c) if(max_so_far < max_ending_here) max_so_far = max_ending_here return max_so_far C++ Code- 
 #include<iostream> #include<climits> using namespace std; //function to calculate max. subarray sum int maxSubArraySum(int a[], int size) { int max_so_far = INT_MIN, max_ending_here = 0; //traversing the array right to left for (int i = size-1; i >= 0; i--) { //finding the maximum subarray sum max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } /*Driver program to find maxSubArraySum*/ int main() { //taking input int n; cin>>n; int a[n+1]; for(int i=0;i<n;i++) cin>>a[i]; //calling the function int max_sum = maxSubArraySum(a, n); cout << "Maximum contiguous sum is " << max_sum; return 0; } 

Input and Output-


If the answer helped then please upvote, it means a lot.
And for any queries feel free to comment.

Add a comment
Know the answer?
Add Answer to:
In class we discussed an approach that yields a Θ(n) algorithm for solving the maximum subarray...
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
  • 2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting...

    2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting the left side, sorting the right side, and then merging them. Write a similar recursive algorithm that finds the maximum element of an array. (Find the max of the left side, then find the maximum of the right side, then compare the two.) Write pseudo-code for this algorithm. Give the recurrence relation that describes the number of comparisons that your algorithm uses.

  • Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This...

    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...

  • In C++: 1. What is the difference between a deterministic algorithm and a randomized algorithm? 2....

    In C++: 1. What is the difference between a deterministic algorithm and a randomized algorithm? 2. What is the difference between a Las Vegas algorithm (pattern) and a Monte Carlo algorithm? 3. Solve the following recurrences, giving the answer in terms of Big-Oh O() F (n) = 4F ( 1 ) +no F (n) = 2 F (*) +2 4. Given the string "DECEMBER", execute the mergesort algorithm by hand to sort it. Show your work to make it clear...

  • I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...

    I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...

  • 2. In class, we discussed the problem of searching for an item x in a sorted...

    2. In class, we discussed the problem of searching for an item x in a sorted array L with n entries. The returned value is supposed to be -1 if the value x is not in the array, and the index i such that L[i] = x if x is in the array. Assume that I does not contain duplicate values. Now consider the decision trees of algorithms for solving this problem. To make the decision tree more explicit, we...

  • ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real...

    ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...

  • I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the...

    I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the maximum-subarray problem. Start at the left end of the array, and progress toward the right, keeping track of the maximum subarray seen so far. Knowing a maximum subarray of A[1..j], extend the answer to find a maximum subarray ending at index j + 1 by using the following observation: a maximum subarray of A[1..j + 1] is either a maximum subarray of A[1..j] or...

  • Suppose you have an array S indexed from 1 to n which contains n numbers, not...

    Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • PLEASE USE MATLAB. The code is basically given to you with the algorithm above. Write a...

    PLEASE USE MATLAB. The code is basically given to you with the algorithm above. Write a program for solving the system Ar-b by using Gauss-Seidel iterative method discussed in class that uses a computer memory for one iterate only. This could be summarized as follows: Input n -- the size of the system, matrix A-(a(i,j), the vectoir b (b (1), . . . , b(n)), the intitial guess x (x(1), ..., x(n)) (you can use x=b) maximum number of iterations...

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