Maximum Sum Contiguous Subsequence
Identify the positive contiguous sub-sequence with maximum sum
Problem Statement:
Given an array of positive and negative integers, identify the sub-sequence (set of contiguous elements) with maximum sum.
Assumptions:
A sub-sequence contains one (or) more elements.
There will always be one positive sub-sequence in the array
Approach 1: Naive Searching
At every index position, check for all possible sub-sequences starting with that index position. Finally, output the sub-sequence with maximum value.
Approach 2: Using Divide and Conquer
Base case : When the array contains only one element, the maximum sum is the element itself
Recursive case: Sub-problems find the maximum sum recorded in a sub-array. To find the solution to the problem, (i.e) To Find the maximum sum sub-sequence for the entire array
It may be a sub-sequence which starts in the left sub-array and ends in the right sub-array. To find this , use O(n) search adding up elements one after other starting from midpoint to the first element in the left sub-array and record the maximum obtained value . Repeat the same from position mid+1 to the last element in the left sub-array
Approach 3: Iterative method
Intuition : negative sub-sequences affect the sum
Maintain sum variable and Iteratively add elements to the sum. Also keep track of the maximum sum recorded. Whenever the sum reaches a negative value, restart the procedure by initializing sum to zero
Time Complexity analysis : Array is scanned only once. Hence records a T(n) = O(n)
Last updated