Longest sequence of positive integers in an array
Last Updated :
28 Oct, 2022
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 a number of positives starting with the positive element picked by outer loop. The 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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getLongestSeq( int a[], int n)
{
int maxIdx = 0, maxLen = 0, currLen = 0, currIdx = 0;
for ( int k = 0; k < n; k++) {
if (a[k] > 0) {
currLen++;
if (currLen == 1)
currIdx = k;
}
else {
if (currLen > maxLen) {
maxLen = currLen;
maxIdx = currIdx;
}
currLen = 0;
}
}
if (currLen > maxLen) {
maxLen = currLen;
maxIdx = currIdx;
}
if (maxLen > 0)
cout << "Length " << maxLen << ", starting index "
<< maxIdx << endl;
else
cout << "No positive sequence detected." << endl;
return ;
}
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;
}
|
Java
import java.io.*;
class GFG {
static void getLongestSeq( int a[], int n)
{
int maxIdx = 0 , maxLen = 0 , currLen = 0 ,
currIdx = 0 ;
for ( int k = 0 ; k < n; k++) {
if (a[k] > 0 ) {
currLen++;
if (currLen == 1 )
currIdx = k;
}
else {
if (currLen > maxLen) {
maxLen = currLen;
maxIdx = currIdx;
}
currLen = 0 ;
}
}
if (currLen > maxLen) {
maxLen = currLen;
maxIdx = currIdx;
}
if (maxLen > 0 ) {
System.out.print( "Length " + maxLen);
System.out.print( ",starting index " + maxIdx);
}
else
System.out.println(
"No positive sequence detected." );
return ;
}
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);
}
}
|
Python3
def getLongestSeq(a, n):
maxIdx = 0
maxLen = 0
currLen = 0
currIdx = 0
for k in range (n):
if a[k] > 0 :
currLen + = 1
if currLen = = 1 :
currIdx = k
else :
if currLen > maxLen:
maxLen = currLen
maxIdx = currIdx
currLen = 0
if currLen > maxLen:
maxLen = currLen
maxIdx = currIdx
if maxLen > 0 :
print ( 'Index : ' , maxIdx, ',Length : ' , maxLen,)
else :
print ( "No positive sequence detected." )
arr = [ 1 , 2 , - 3 , 2 , 3 , 4 , - 6 , 1 ,
2 , 3 , 4 , 5 , - 8 , 5 , 6 ]
n = len (arr)
getLongestSeq(arr, n)
|
C#
using System;
class GFG {
static void getLongestSeq( int [] a, int n)
{
int maxIdx = 0, maxLen = 0, currLen = 0,
currIdx = 0;
for ( int k = 0; k < n; k++) {
if (a[k] > 0) {
currLen++;
if (currLen == 1)
currIdx = k;
}
else {
if (currLen > maxLen) {
maxLen = currLen;
maxIdx = currIdx;
}
currLen = 0;
}
}
if (currLen > maxLen) {
maxLen = currLen;
maxIdx = currIdx;
}
if (maxLen > 0) {
Console.Write( "Length " + maxLen);
Console.WriteLine( ",starting index " + maxIdx);
}
else
Console.WriteLine( "No positive sequence"
+ " detected." );
return ;
}
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);
}
}
|
PHP
<?php
function getLongestSeq( $a , $n )
{
$maxIdx = 0;
$maxLen = 0;
$currLen = 0;
$currIdx = 0;
for ( $k = 0; $k < $n ; $k ++) {
if ( $a [ $k ] > 0) {
$currLen ++;
if ( $currLen == 1)
$currIdx = $k ;
}
else {
if ( $currLen > $maxLen ) {
$maxLen = $currLen ;
$maxIdx = $currIdx ;
}
$currLen = 0;
}
}
if ( $currLen > $maxLen ) {
$maxLen = $currLen ;
$maxIdx = $currIdx ;
}
if ( $maxLen > 0)
echo "Length " . $maxLen .
", starting index " .
$maxIdx . "\n" ;
else
echo "No positive sequence detected." . "\n" ;
return ;
}
$arr = array (1, 2, -3, 2, 3, 4, -6,
1, 2, 3, 4, 5, -8, 5, 6);
$n = count ( $arr );
getLongestSeq( $arr , $n );
?>
|
Javascript
<script>
function getLongestSeq(a, n)
{
let maxIdx = 0, maxLen = 0, currLen = 0, currIdx = 0;
for (let k = 0; k < n; k++)
{
if (a[k] > 0)
{
currLen++;
if (currLen == 1)
currIdx = k;
}
else
{
if (currLen > maxLen)
{
maxLen = currLen;
maxIdx = currIdx;
}
currLen = 0;
}
}
if (currLen > maxLen){
maxLen = currLen;
maxIdx = currIdx;
}
if (maxLen > 0)
{
document.write( "Length " + maxLen) ;
document.write( ", starting index "
+ maxIdx + "</br>" );
}
else
document.write( "No positive sequence"
+ " detected." ) ;
return ;
}
let arr = [ 1, 2, -3, 2, 3, 4, -6,
1, 2, 3, 4, 5, -8, 5, 6 ];
let n = arr.length;
getLongestSeq(arr, n);
</script>
|
Output
Length 5, starting index 7
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...