algorithm
contiguous subsequence
time complexity
minimum subsequence
O(n)

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.

Practice algorithms

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

  1. 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.
  2. 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.
  3. 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:

3,1,4,2,5,9,33, -1, 4, -2, 5, -9, 3

  1. Initialization:
    • current_sum = 0
    • min_positive_sum = +\infty
    • positive_found = False
  2. Iterate through each element:
    • Element: 3
      current_sum = 3
      min_positive_sum = 3
      positive_found = True
    • Element: -1
      current_sum = 2
      min_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).
  3. Final Result:
    • Minimum positive contiguous subsequence sum is 2, which is the subsequence 3,13, -1.

Pseudocode


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.