How to find minimum positive contiguous sub sequence in On time?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
To find the minimum positive contiguous subsequence in an array in O(n) time, we can leverage a variant of Kadane's algorithm, which is typically used to find the maximum sum subarray. We focus on identifying the smallest positive sum subarray instead. This solution requires careful tracking of sums to isolate the smallest positive segment efficiently.
Algorithm Overview
- Initialization:
- Start with an accumulator to keep track of the current subarray sum.
- Use a variable to store the minimum positive sum found so far.
- A flag is set to check whether any positive sum has been encountered.
- Iterate through each element:
- Add current element to the current sum.
- Check if the current sum is positive and smaller than the current minimum positive sum, update the minimum positive sum.
- If the current sum becomes non-positive, reset it to zero since we are looking for positive sums.
- This approach will ensure that you traverse each element once, giving you an O(n) complexity.
- Edge Cases:
- If no positive sum is found throughout the array, it's essential to handle this scenario separately by checking the flag.
Step-by-Step Example
Let's consider an example to demonstrate the approach:
Example Array:
- Initialization:
current_sum = 0min_positive_sum = +\inftypositive_found = False
- Iterate through each element:
- Element: 3
current_sum = 3min_positive_sum = 3positive_found = True - Element: -1
current_sum = 2min_positive_sum = 2 - Element: 4
current_sum = 6- No update, as 6 is not less than 2.
- Element: -2
current_sum = 4- No update, as 4 is not less than 2.
- Element: 5
current_sum = 9- No update as 9 is not less than 2.
- Element: -9
current_sum = 0- Reset since the sum has become non-positive.
- Element: 3
current_sum = 3- No update as 3 is not less than 2 (but at this position, the current sum restarts).
- Final Result:
- Minimum positive contiguous subsequence sum is 2, which is the subsequence .
Pseudocode
Related reading
- How to find mother vertex in a directed graph in Onm?
- How to find multidimensional path of exact 0 cost with 1, 0, -1 weights
- How to find nth element from the end of a singly linked list?
- How to find out Geometric Median
- How to find optimum combination for Cutting Stock Problem using Knapsack
- How to find overall CPU usage in a multi-tenant environment?
- How to find out if an item is present in a stdvector?
- How to find pairs with product greater than sum

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.