I have to fing an increasing non-contigious subarray with maximum sum using Python. For example, if I have an array [5, 2, 4, 3, 7, 7], than I have to output [2, 4, 7]. Output given is increasing, have the maximum sum among all the possible increasing subarrays.
Asked
Active
Viewed 72 times
1
-
Hi, which is the logic behind this output? – Carlo Zanocco Sep 29 '20 at 12:56
-
1@CarloZanocco A non decreasing subsequence with maximum sum, although the question is poorly written. – nice_dev Sep 29 '20 at 12:58
-
@fiendfire2807 can you tell me if I have correctly understood your question? – Carlo Zanocco Sep 29 '20 at 13:17
-
Does this answer your question? [Maximum Sum of a non decreasing sub-sequence in an array using fenwick tree or BIT](https://stackoverflow.com/questions/15193469/maximum-sum-of-a-non-decreasing-sub-sequence-in-an-array-using-fenwick-tree-or-b) – Stef Sep 29 '20 at 14:20
-
1The accepted answer has complexity O(n^2) both in time and space, but this question is actually a duplicate and the previous question has an answer is O(n log n) time and O(n) space: [maximum sum of a non-decreasing sub-sequence in an array](https://stackoverflow.com/questions/15193469/maximum-sum-of-a-non-decreasing-sub-sequence-in-an-array-using-fenwick-tree-or-b) – Stef Sep 29 '20 at 14:21
-
@Stef The question that you consider duplicate require another type of output, here we need a list not the max value as int – Carlo Zanocco Sep 29 '20 at 15:48
-
@CarloZanocco Those two problems are pretty close; deriving the "path" to the optimal solution found with a dynamic programming algorithm is usually very easy, by tracing the optimal values in the array used by the algorithm. – Stef Sep 29 '20 at 16:19
-
@Stef Have you tried to run the code? `[5, 2, 4, 3, 7, 7]` return `20` insed of `13` – Carlo Zanocco Sep 29 '20 at 16:58
1 Answers
1
Take a look at this code:
def elaborate(A):# Iterative function to princreasing subsequence with the maximum sum
n = len(A)
# values[i] stores the increasing subsequence having maximum sum
# that ends with A[i]
values = [[] for _ in range(n)]
values[0].append(A[0])
# sum[i] stores the maximum sum of the increasing subsequence
# that ends with A[i]
sum = [0] * n
sum[0] = A[0]
for i in range(1, n): # start from second element in the list
for j in range(i):# do for each element in sublist[0..i-1]
# find increasing subsequence with maximum sum that ends with
# A[j] where A[j] is less than the current element A[i]
if sum[i] < sum[j] and A[i] > A[j]:
values[i] = values[j].copy() # update increasing subsequence
sum[i] = sum[j] # update maximum sum
values[i].append(A[i]) # include current element in increasing subsequence
sum[i] += A[i] # add current element to maximum sum
j = 0 # j will contain index of values
for i in range(1, n):
if sum[i] > sum[j]:
j = i
# print values
print(values[j])
A = [5, 2, 4, 3, 7, 7]
elaborate(A)
# OUTPUT: [2, 4, 7]
The time complexity of above solution is O(n^2)
and auxiliary space used by the program is O(n^2)

Carlo Zanocco
- 1,967
- 4
- 18
- 31