DEV Community

Shahrouz Nikseresht
Shahrouz Nikseresht

Posted on

Day 52: Python Longest Continuous Increasing Subarray – Find the Maximum Length of Strictly Increasing Adjacent Elements in O(n)

Welcome to Day 52 of the #80DaysOfChallenges journey! This intermediate challenge tackles the Longest Continuous Increasing Subsequence (LCIS) problem, where you scan an array to find the longest stretch of adjacent elements that are strictly increasing (each next element > previous). The solution uses a simple linear pass with two variables to track current and maximum streak lengths, achieving optimal O(n) time and O(1) space while handling all edge cases perfectly.

Example:

[1,3,5,4,7] → 3 (from 1,3,5)

[2,2,2,2,2] → 1

[1,3,5,7] → 4

If you're practicing array traversal, preparing for problems like LeetCode #674 (Longest Continuous Increasing Subsequence), or analyzing sequences in data streams, this "Python longest increasing subarray" solution is clean, blazing fast, and the exact pattern used in real-world applications like finding longest rising trends in stock prices or sensor readings.


💡 Key Takeaways from Day 52: LCIS Function

This task delivers an elegant single-pass solution that compares neighbors and maintains streaks with reset on breaks. It's a fundamental streak-tracking pattern seen in many array problems. We'll detail: function with streak initialization, loop for comparison and updates, and main with input handling.

1. Function Design: Edge Case and Streak Setup

def find_longest_increasing_subarray(nums: list[int]) -> int:
    """
    Return the length of the longest continuous strictly-increasing subsequence.
    Uses a simple linear scan comparing each element with its previous one.
    """
    if not nums:
        return 0

    max_streak = 1    # longest streak found so far
    current_streak = 1    # length of the current increasing sequence
Enter fullscreen mode Exit fullscreen mode

Handles empty list immediately. Initializes both streaks to 1 because a single element is always a valid increasing subarray of length 1.

2. Loop Logic: Neighbor Check, Extend or Reset

    # iterate from the second element onward
    for i in range(1, len(nums)):

        # check if current number is strictly increasing
        if nums[i] > nums[i - 1]:
            current_streak +=  # extend current streak
        else:
            current_streak = 1    # reset when sequence breaks

        # update maximum if needed
        if current_streak > max_streak:
            max_streak = current_streak

    # final length of longest increasing streak
    return max_streak
Enter fullscreen mode Exit fullscreen mode

Starts from index 1 to compare with previous. If strictly greater (>), extend current_streak; else reset to 1. Updates max_streak whenever current exceeds it. For [1,3,5,4,7]: current goes 1→2→3 (at 5), resets to 1 (at 4), extends to 2 (at 7) → max=3.

Strict > ensures no plateaus count as increasing.

3. Main Interactive: Input Parsing and Result

raw_input_nums = input("Enter numbers (space-separated): ").strip()

if not raw_input_nums:
    print("No numbers provided. Exiting.")
else:
    nums = list(map(int, raw_input_nums.split()))
    result = find_longest_increasing_subarray(nums)
    print("Length of the longest continuous increasing subsequence:", result)
Enter fullscreen mode Exit fullscreen mode

Gracefully handles empty input, splits space-separated numbers to list, prints clear result. Perfect for quick testing.


🎯 Summary and Reflections

This LCIS solution is minimalistic yet powerful, using just two variables for perfect O(n) efficiency. It reinforced:

  • Neighbor comparison pattern: Core for many sequence problems.
  • Streak tracking: Current/max variables for ongoing records.
  • Strict vs non-strict: > for strictly increasing (change to >= for non-decreasing).

Reflections: Applies everywhere from financial trends to game scores. For the actual subarray (not just length), track start index too.

Advanced Alternatives: One variable version (reset max on break). Or return start/end indices. Your longest streak found? Share!


🚀 Next Steps and Resources

Day 52 mastered sequence scanning. Try [5,4,3,2,1,6,7,8,9] → 4

What array gave you the longest streak? Drop it below! 🔥

Top comments (0)