Longest Positive Subarray
Array Algorithms
Subarray Problems
Positive Integers
Data Structures

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.

Practice algorithms

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:

arr=[2,3,5,4,6,7,1,8,10,3]\text{arr} = [-2, 3, 5, -4, 6, 7, -1, 8, 10, -3]

The positive subarrays in this array are:

  1. [3,5][3, 5]
  2. [6,7][6, 7]
  3. [8,10][8, 10]

The task is to identify which of these has the longest length. In this instance, the answer is [8,10][8, 10], 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 O(n)O(n), where nn is the number of elements in the array. Here is a step-by-step explanation of the algorithm:

  1. 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.
  2. 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.
  3. 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: O(n)O(n) since the solution requires a single pass through the array. • Space Complexity: O(1)O(1) 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
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.