Longest sequence of positive integers in an array

Find the longest running positive sequence in an array.

Examples:

Input : arr[] = {1, 2, -3, 2, 3, 4, -6, 1, 
                     2, 3, 4, 5, -8, 5, 6}
Output :Index : 7, length : 5

Input : arr[] = {-3, -6, -1, -3, -8}
Output : No positive sequence detected.

A simple solution is to use two nested loops. In the outer loop, we look for positive elements. In the inner loop, we count number of positives starting with the positive element picked by outer loop. Time complexity of this solution is O(n^2).

An efficient solution is to use one single loop. We keep incrementing count while we see positive integers. We reset count to 0 after seeing a negative. Before resetting, we check if count is more than maximum.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find longest running sequence
// of positive integers.
#include <bits/stdc++.h>
using namespace std;
  
// Prints longest sequence of positive integers in
// an array.
void getLongestSeq(int a[], int n)
{
    // Variables to keep track of maximum length and 
    // starting point of maximum length. And same 
    // for current length.
    int maxIdx = 0, maxLen = 0, currLen = 0, currIdx = 0;
  
    for (int k = 0; k < n; k++) {
        if (a[k] > 0) {
            currLen++;
  
            // New sequence, store
            // beginning index.
            if (currLen == 1) 
                currIdx = k;            
        }
        else {
            if (currLen > maxLen) {
                maxLen = currLen;
                maxIdx = currIdx;
            }
            currLen  = 0;
        }
    }
  
    if (maxLen > 0) 
        cout << "Length " << maxLen 
             << ", starting index " << maxIdx << endl;    
    else 
        cout << "No positive sequence detected." << endl;
      
    return;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, -3, 2, 3, 4, -6, 
               1, 2, 3, 4, 5, -8, 5, 6 };
    int n = sizeof(arr) / sizeof(int);
    getLongestSeq(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find longest running 
// sequence of positive integers
import java.io.*;
  
class GFG {
      
    // Prints longest sequence of 
    // positive integers in an array.
    static void getLongestSeq(int a[], int n)
    {
        // Variables to keep track of maximum
        // length and  starting point of 
        // maximum length. And same for current
        // length.
        int maxIdx = 0, maxLen = 0, currLen = 0, currIdx = 0;
      
        for (int k = 0; k < n; k++) 
        {
            if (a[k] > 0)
            {
                currLen++;
      
                // New sequence, store
                // beginning index.
                if (currLen == 1
                    currIdx = k;         
            }
            else
            {
                if (currLen > maxLen) 
                {
                    maxLen = currLen;
                    maxIdx = currIdx;
                }
                currLen = 0;
            }
        }
      
        if (maxLen > 0
        {
            System.out.print( "Length " + maxLen) ; 
            System.out.print( ",starting index " + maxIdx );
        }
        else
            System.out.println("No positive sequence detected.") ;
          
        return;
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        int arr[] = { 1, 2, -3, 2, 3, 4, -6
                      1, 2, 3, 4, 5, -8, 5, 6 };
        int n = arr.length;
        getLongestSeq(arr, n);
      
    }
}
  
// This article is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to find longest running 
# sequence of positive integers.
  
def getLongestSeq(a, n):
    maxIdx = 0
    maxLen = 0
    currLen = 0
    currIdx = 0
    for k in range(n):
        if a[k] > 0:
            currLen +=1
  
            # New sequence, store
            # beginning index.
            if currLen == 1:
                currIdx = k
        else:
            if currLen > maxLen:
                maxLen = currLen
                maxIdx = currIdx
            currLen = 0
              
    if maxLen > 0:
        print('Index : ',maxIdx,',Length : ',maxLen,)
    else:
        print("No positive sequence detected.")
  
# Driver code
arr = [ 1, 2, -3, 2, 3, 4, -6
        1, 2, 3, 4, 5, -8, 5, 6
n = len(arr)
getLongestSeq(arr, n)
  
# This code is contributed by "Abhishek Sharma 44"

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find longest running
// sequence of positive integers.
using System;
  
class GFG {
      
    // Prints longest sequence of 
    // positive integers in an array.
    static void getLongestSeq(int []a, int n)
    {
          
        // Variables to keep track of maximum
        // length and starting point of 
        // maximum length. And same for current
        // length.
        int maxIdx = 0, maxLen = 0, currLen = 0,
            currIdx = 0;
      
        for (int k = 0; k < n; k++) 
        {
            if (a[k] > 0)
            {
                currLen++;
      
                // New sequence, store
                // beginning index.
                if (currLen == 1) 
                    currIdx = k;         
            }
            else
            {
                if (currLen > maxLen) 
                {
                    maxLen = currLen;
                    maxIdx = currIdx;
                }
                currLen = 0;
            }
        }
      
        if (maxLen > 0) 
        {
            Console.Write( "Length " + maxLen) ; 
            Console.WriteLine( ",starting index "
                                      + maxIdx );
        }
        else
            Console.WriteLine("No positive sequence"
                                   + " detected.") ;
          
        return;
    }
      
    // driver code
    public static void Main()
    {
        int []arr = { 1, 2, -3, 2, 3, 4, -6, 
                    1, 2, 3, 4, 5, -8, 5, 6 };
        int n = arr.Length;
        getLongestSeq(arr, n);
          
    
  
}
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find longest running
// sequence of positive integers.
  
// Prints longest sequence of positive 
// integers in an array.
function getLongestSeq($a, $n)
{
      
    // Variables to keep track 
    // of maximum length and 
    // starting point of maximum
    // length. And same for 
    // current length.
    $maxIdx = 0;
    $maxLen = 0;
    $currLen = 0;
    $currIdx = 0;
  
    for ($k = 0; $k < $n; $k++) {
        if ($a[$k] > 0) {
            $currLen++;
  
            // New sequence, store
            // beginning index.
            if ($currLen == 1) 
                $currIdx = $k;         
        }
        else {
            if ($currLen > $maxLen) {
                $maxLen = $currLen;
                $maxIdx = $currIdx;
            }
            $currLen = 0;
        }
    }
  
    if ($maxLen > 0) 
        echo "Length " . $maxLen
             ", starting index "
             $maxIdx ."\n"
    else
        echo "No positive sequence detected."."\n";
      
    return;
}
  
    // Driver Code
    $arr = array(1, 2, -3, 2, 3, 4, -6, 
                 1, 2, 3, 4, 5, -8, 5, 6);
    $n = count($arr);
    getLongestSeq($arr, $n);
      
// This code is contributed by Sam007
?>

chevron_right



Output:

Length 5, starting index 7

The algorithm is O(n) time and O(1) auxiliary space.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Sam007



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.