Question

Use the dynamic programming approach to write an algorithm to find the maximum sum in any...

  1. Use the dynamic programming approach to write an algorithm to find the maximum sum in any contiguous sublist of a given list of n real values. Analyze your algorithm, and show the results using order notation.

Homework Answers

Answer #1

Algorithm:

->

Initialize:

-MAX=a[0]

-CURRENT MAX=0

-LOOP:

-for each element of array starting from 1 to end of array

      current max=(find maximum from either (array[i]) or (CURRENT MAX+array[i])

       MAX=find maximum from (CURRENT MAX) or (CURRENT MAX + MAX)

end loop

print MAX is answer

STEPS:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

PROGRAM:

#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n;
   cout<<"Enter the size of array:";
   cin>>n;
   int a[n];
   for(int i=0;i<n;i++)
       cin>>a[i];
      
int MAX = a[0];
int CURRENT_MAX = a[0];

for (int i = 1; i < n; i++)
{
       CURRENT_MAX = max(a[i], CURRENT_MAX+a[i]);//check weather previous step is greater or CURRENT_MAX+rray[i]
      
       MAX = max(MAX, CURRENT_MAX); //MAximise MAX with either previous MAX or CURRENT_MAX
      
}
cout<<MAX;
}

OUTPUT:

Know the answer?
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
Similar Questions
Do the following problem by developing a dynamic programming algorithm for it: Describe efficient algorithms for...
Do the following problem by developing a dynamic programming algorithm for it: Describe efficient algorithms for the following variants of the text segmentation problem. Assume that you have a subroutine IsWord that takes an array of characters as input and returns True if and only if that string is a “word”. Analyze your algorithms by bounding the number of calls to IsWord. Given an array A[1 .. n] of characters, compute the number of partitions of A into words. For...
Given a directed acyclic graph G= (V,E), vertex s∈V, design a dynamic programming algorithm to compute...
Given a directed acyclic graph G= (V,E), vertex s∈V, design a dynamic programming algorithm to compute the number of distinct paths from s to v for any v∈V. 1. Define subproblems 2. Write recursion 3. Give the pseudo-code 4. Analyze the running time.
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in O(N) time. You can use as much extra space as you need. The original list pointers CAN NOT BE MODIFIED. State in big-O notation how much extra space is used by this algorithm. Write another iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse using O(1) extra space. The original list pointers CAN NOT BE MODIFIED. This algorithm can have...
5.Write pseudocode for the algorithm using iteration (looping) and make a flow chart and python code...
5.Write pseudocode for the algorithm using iteration (looping) and make a flow chart and python code from your pseudocode. Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. For example given 3 the algorithm will print 14 (because 1 + 4 + 9 =14) You can use multiplication denoted as * in your solution and you do not have to define it (For example. 3*3=9) For...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward solutions. One of those problems is the “Maximum Subarray Sum” or “Maximum Value Contiguous Subsequence": Given a sequence of n numbers A[0 ... n-1], give an algorithm for finding a contiguous subsequence A[i ... j], for which the sum of elements in this subsequence is the maximum we can get by choosing different i or j.   For example: the maximum subarray sum for the...
1) Use the First Derivative Test to find the local maximum and minimum values of the...
1) Use the First Derivative Test to find the local maximum and minimum values of the function. (Enter your answers as a comma-separated list. If an answer does not exist, enter DNE.): g(u) = 0.3u3 + 1.8u2 + 146 a) local minimum values:    b) local maximum values:    2) Consider the following: f(x) = x4 − 32x2 + 6 (a) Find the intervals on which f is increasing or decreasing. (Enter your answers using interval notation.) increasing:    decreasing:...
Find the absolute maximum and minimum values of the following function over the given interval. If...
Find the absolute maximum and minimum values of the following function over the given interval. If there are multiple points in a single category list the points in increasing order in x value and enter N in any blank that you don't need to use. ?(?)=(10cos?)/(18+9sin?), 0≤?≤2?
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Find the absolute maximum and minimum values of the following function on the given interval. If...
Find the absolute maximum and minimum values of the following function on the given interval. If there are multiple points in a single category list the points in increasing order in x value and enter N in any blank that you don't need to use. f(x)=3(x2−1)3, [−1,2] Absolute maxima x = y = x = y = x= y = Absolute minima x = y = x = y = x = y =
Use a graphing calculator Riemann Sum (found here) to find the following Riemann sums. f(x) =...
Use a graphing calculator Riemann Sum (found here) to find the following Riemann sums. f(x) = 2/x   from  a = 1  to  b = 5 (a) Calculate the Riemann sum for the function for the following values of n: 10, 100, and 1000. Use left, right, and midpoint rectangles, making a table of the answers, rounded to three decimal places. n Left Midpoint Right 10 100 1000 (b) Find the exact value of the area under the curve by evaluating an appropriate definite...