Let's say you are given a sequence of distinct positive numbers. We want to find a subsequence with the maximum possible sum, with the restriction that we are not allowed to take three consecutive elements from the original sequence. For example, for input 1, 6, 5, 2, 7, 9, 3, 4, the subsequence with the maximum possible sum is 6, 5, 7, 9, 4 (we have two pairs of consecutive elements 6, 5 and 7, 9 but not three consecutive elements).
Consider the following greedy strategies:
• Start with an empty subsequence. Go through the numbers in the sequence in decreasing order. For every number, if it does not cause three consecutive elements in the subsequence, include it in the subsequence (otherwise skip the number). For our example, we include 9, 7, 6, 5, 4, then we skip 3, 2, and 1.
• Go through the numbers in the sequence in increasing order, crossing them out until you get a sequence without three consecutive elements. For our example, we cross out 1, 2, and 3.
• Split the input into groups of three elements (the last group may have one or two elements). Go through the groups from left to right. For each group find the maximum element and include it in the subsequence. After that step is completed, go through the remaining elements in decreasing order, including an element if it does not cause three consecutive elements in the subsequence (otherwise skipping the element). For our example, the groups are 1, 6, 5 and 2, 7, 9 and 3, 4. We include 6, 9 and 4 in the subsequence. Then we include 7 and 5, and skip 3, 2, and 1.
None of these greedy strategies always produces the optimal answer. For each greedy strategy, provide a counterexample input, along with an optimum solution and its sum (for this input), and the solution produced by the greedy algorithm and its sum (for this input).
Give an O(n) dynamic programming solution to this problem (assume input sequence of length n).
Using Dynamic Programming
(1)create an auxiliary array DP[] (of same size as input array) to
find the result.
DP[i] : Stores result for subarray arr[0..i], i.e.,
maximum possible sum in subarray arr[0..i]
such that no three elements are consecutive.
base case:
DP[0] = arr[0]
DP[1] = arr[0] + arr[1]
DP[2] = max(sum[1], arr[0] + arr[2], arr[1] + arr[2])
DP[i] = max(DP[i-1], DP[i-2] + arr[i],DP[i-3] + arr[i] + arr[i-1])
Algorithm:
int DP[n];
// Base cases (process first three elements)
if (n >= 1)
DP[0] = arr[0];
if (n >= 2)
DP[1] = arr[0] + arr[1];
if (n > 2)
DP[2] = max(DP[1], max(arr[1] + arr[2], arr[0] + arr[2]));
for (int i = 3; i < n; i++)
DP[i] = max(DP[i-1], DP[i-2] + arr[i],DP[i-3] + arr[i] +
arr[i-1])
return value of DP[n-1]
Time complexity =O(n)
Let's say you are given a sequence of distinct positive numbers. We want to find a...
(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...
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.
Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a...
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...
5) (10 pts) Greedy Algorithms The 0-1 Knapsack problem is as follows: you are given a list of items, each item has an integer weight and integer value. The goal of the problem is to choose a subset of the items which have a sum of weights less than or equal to a given W with a maximal sum of values. For example, if we had the following five items (each in the form (weight, value)): 11(6, 13), 2(4, 10),...
You are given a sequence of positive real numbers a[1..n]. You can now add ‘+’ and ’×’ signs between these numbers, and your goal is to generate an expression that has the largest value. As an example, if a = {2, 3, 0.5, 2}, then you should output the expression 2 × 3 + 0.5 + 2 = 8.5. This is larger than any other expression (e.g. 2 × 3 × 0.5 × 2 = 6, 2 + 3 +...
Write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array. For example, if the program is executed with the input data: 2 1 4 9 16 9 7 4 9 11 Then it computes 2 - 1 + 4 - 9 + 16 - 9 + 7 - 4 + 9 – 11 = 4 Use a scanner object to gather the inputs from the user....
Suppose we select some initial number ? and then build the sequence of values by the following 0 Hint: each time you find a multiple of each picked number in the initial list [1:n], make it zero. Elements not equal to zero are primes. This works a lot better than trying to ‘erase’ the element. Because each time you erase an element the remaining indexes roll over, i.e., they change. 1. Find the possible ranges of a and b for...
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...