5283. Thaw

 

Tired of the unusually warm winter, the residents of Frostholm decided to determine whether the current thaw is indeed the longest in the entire history of meteorological observations. They turned to meteorologists, who in turn began analyzing statistical data from previous years. They are interested in how many days the longest thaw lasted.

A thaw is defined as a period during which the average daily temperature exceeded 0 degrees Celsius every day. Write a program that will help the meteorologists solve this problem.

 

Input. The first line contains the number of days n (1 ≤ n ≤ 1000) under consideration.

The second line contains  n integers – the average daily temperatures for the corresponding days. The temperatures are integers in the range from -50 to 50.

 

Output. Print one integer – the length of the longest thaw, that is, the maximum number of consecutive days during which the average daily temperature exceeded 0 degrees. If the temperature was non-positive on all days, print 0.

 

Sample input

Sample output

6

-20 30 0 50 10 -10

2

 

 

SOLUTION

arrays

 

Algorithm analysis

The variable res stores the length of the longest thaw.

The variable temp stores the length of the current thawthat is, the number of consecutive days with a positive temperature.

For each day, the temperature value is analyzed:

·        if the temperature is positive, the value of temp is increased by 1;

·        otherwise, temp is reset to 0.

During the processing of all days, the maximum among the values of temp is maintained and stored in the variable res.

 

Example

Let us consider an example.

 

Algorithm implementation

Read the number of days n.

 

scanf("%d", &n);

 

The variable res stores the length of the longest thaw.

The variable temp stores the length of the current thaw (the number of consecutive days with a positive temperature).

 

temp = res = 0;

 

Process the temperatures for all n days.

 

for (i = 0; i < n; i++)

{

  scanf("%d", &cur);

 

If cur > 0, increase the length of the current thaw by 1. Otherwise, reset its value to 0.

 

  if (cur > 0) temp++; else temp = 0;

 

Among all obtained values of the current thaw length, we maintain the maximum value.

 

  if (temp > res) res = temp;

}

 

Print the answer.

 

printf("%d\n", res);