P2B - Editorial

PROBLEM LINK:
Practice
Source

Author: SQU_Test

DIFFICULTY:
Medium

PREREQUISITES:
Dynamic programming, greedy.

PROBLEM:
You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.

A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray is strictly greater than previous.

EXPLANATION:
Let’s iterate through the given array from the left to the right and store in the variable c the length of the current increasing subarray. If the current element is more than the previous we need to increase c by one. In the other case we need to update the answer with the value of c and put in maxC and reset c to 1, because new increasing subarray began.

TIME COMPLEXITY:
Time complexity of the solution is O(n).

SOLUTIONS:

Setter’s Solution
    #include <iostream>
    using namespace std;

    int main()
    {
            long n,prev,c,cur,maxC;
            cin>>n;
            cin>> prev;
            c = 1;
            maxC = 1;
            for(int i =0;i<n-1;i++)
            {
                    cin>>cur;
                    if(cur > prev)
                        c++;
                    else
                    {
                        if(c>maxC)
                            maxC=c;
                        c=1;
                    }
                    prev = cur;
            }
            if(c > maxC)
                    maxC = c;

          cout<<maxC<<endl;
        return 0;
    }