Longest positive subarray
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In the field of computer science, particularly within data structures and algorithms, solving for specific patterns or characteristics in arrays is a common task. One such problem is finding the longest positive subarray within a given array of integers. This problem involves identifying the contiguous segment of an array where all the elements are positive and with the maximum possible length.
Problem Definition
Given an array of integers, the objective is to find the contiguous subarray that contains only positive integers and has the maximum length. This problem can demonstrate the synergy between algorithm optimization and data manipulation skills.
Example
Consider the following array:
The positive subarrays in this array are:
The task is to identify which of these has the longest length. In this instance, the answer is , which consists of two elements.
Algorithm and Implementation
Finding the longest positive subarray can be efficiently accomplished using a single-pass algorithm with a time complexity of , where is the number of elements in the array. Here is a step-by-step explanation of the algorithm:
- Initialize Counters:• `max_len` to store the maximum length found. Initialize it to 0. • `current_len` to store the length of the current positive segment being evaluated. Initialize it to 0.
- Traverse the Array:• Iterate over each element of the array. • If the element is positive, increment the `current_len`. • If the element is non-positive, compare `current_len` with `max_len` and update `max_len` if `current_len` is greater. Reset `current_len` to 0.
- Final Evaluation:• After the loop, a final check is necessary in case the longest positive subarray is at the end of the array.
Python Implementation
• Time Complexity: since the solution requires a single pass through the array. • Space Complexity: because no additional space is allocated depending on the size of the input. • All Positive or All Negative: • If the entire array is positive, the longest positive subarray is the array itself. • If the array lacks positive numbers, the longest positive subarray length is zero. • Empty Array: • The edge case of an empty input array should return a result of zero for the maximum length, as there are no elements to analyze. • Finding the longest subarray of negatives • Longest subarray of even/odd numbers • Subarrays with a sum greater than a given threshold
Related reading
- Longest positive sum substring
- Longest subArray with no more than two distinct values that differ by no more than 1
- Longest Subsequence with all occurrences of a character at 1 place
- Longest substring that occurs at least twice C question
- Longest substring where every character appear even number of times possibly zero
- Looking for distributed, in-memory Graph DB
- Looking for a better evaluation method for a genetic algorithm
- Looking for a good world map generation algorithm

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.