Positive streak

Given a list of positive and negative integers that were arranged at random it is required to find the longest positive streak, that is the longest sub-sequence of positive numbers.

What is the length longest positive streak in text file.

Details and Assumptions

-In the array [ 1 , 4 , 3 , 2 , 4 ] [-1, 4, 3, -2, 4] the longest positive streak is of length 2 2 ... [ 4 , 3 ] [4, 3]

- 0 0 is a middle ground and can count as both a positive and negative streak.


The answer is 14.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

5 solutions

Bill Bell
Aug 8, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def item():
    for s in [-5, 6, 0, -5, -7, -3, -2, 1, 6, -6, 3, 0, 0, -2 ... -1, -5]:
        if s==0: yield 0
        elif s>0: yield 1
        else: yield -1

state=-1
maxCount=0
for it in item():
    if state==-1:
        if it==1:
            count=1
            state=1
    else:
        if it in [0,1]:
            count+=1
        else:
            maxCount=max(maxCount,count)
            state=-1
print maxCount

Arulx Z
Aug 7, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
lar = 0
counter = 0
prev = False

for x in [that array]:
    if prev:
        if x >= 0:
            counter += 1
        else:
            lar = counter if counter > lar else lar
            counter = 0
            prev = False
        continue

    if x >= 0:
        prev = True
        counter += 1

print counter if counter > lar else lar

Masbahul Islam
Aug 16, 2016

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
streak = 0;
maxs = 0;
for x in l:
    if( x < 0 ):
        if( streak > maxs ):
            maxs = streak
        streak = 0
    else:
        streak = streak + 1

print maxs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

int array1[] = { Just paste the array here };

int main()
{
    int array1_len, max_streak, i, temp;

    array1_len = sizeof(array1)/sizeof(array1[0]);
    // length of array1

    max_streak = 0;
    // records the (+) longest streak
    temp = 0;

    for( i = 0 ; i < array1_len ; i++ )
    {
        if( array1[i] < 0 )
        {
            if(temp > max_streak)
                max_streak = temp;
                temp = 0;
            continue;    
        }
        else
            temp++;
    }
    std::cout << max_streak << std::endl;
    return 0;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...