Question

Sequence a1,a2……an consists of nonnegative integers. We want to determine the largest sum of such subsequences...

Sequence a1,a2……an consists of nonnegative integers. We want to determine the largest sum of such subsequences that do not contain two consecutive elements of the original sequence (For example if a3 is a member of the subsequence, then a2 and a4 cannot be contained in the subsequence ). Give an O(n) running time algorithms for this problem.

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

Given sequence a1,a2.....an.
To find maximum sum of subsequence that do not contain two consecutive elements.
We will use Dynamic programming to solve given problem.

Algorithm:
Create an array DP[1...n] of size n that stores maximum sum of subsequence that do not contain two consecutive elements.
for i=1 to n
if(i==1) DP[i] = a[i];
if(i==2) DP[i] = maximum(DP[0],a[1]);
else DP[i] = maximum(DP[i-2]+a[i],DP[i-1]);

print DP[n] will give maximum sum of subsequence that do not contain two consecutive elements.
Time complexity = O(n)

Add a comment
Know the answer?
Add Answer to:
Sequence a1,a2……an consists of nonnegative integers. We want to determine the largest sum of such subsequences...
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
  • (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum...

    (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence. Input The input consists of multiple datasets. Each data set consists of: n a1 a2 . . an You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000. The input end with a line consisting of a single 0. Output...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • Your goal is to create an ‘Array’ class that is able to hold multiple integer values....

    Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

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