ICD02-Editorial

#PROBLEM LINK:

[problem][1]

Author: Yasha Jadwani

Tester : Prabhsimran Kaur

Editorialist: Prabhsimran Kaur

DIFFICULTY :

Simple

PREREQUISITES :

Math

PROBLEM:

Given is an array where each element denote the height of a building. There can be 3 possible cases:

  1. As you encounter a building with height greater than the current building, you need a ladder.
  2. If you encounter a building with smaller height than the current building, you don’t need a ladder.
  3. Also, in the case of equal heights, no ladder is required.
    So, you need to find the number of ladders that are required to go from one building to another.
    EXPLANATION:
    Given the above cases, we can find the number of ladders required using simple if condition.
    All that has to be done is
  4. As you encounter a building with a height greater than the current building, increase a counter by one (as this counter keeps the track of number of ladders required).
  5. And each time you encounter a building which has height greater than the current one. Then, make it as maximum height, so that the next comparison you perform will consider this height.

For better understanding, consider an example where the array is:
10, 9, 12, 11, 14.

Now, initially we will consider maximum height (max) as 10, i.e, first element of array.
To move from 10 to 9 ladder is not required, so counter(c=0) is 0.

The next comparison is between 10(max) and 12. Hence, a ladder is required , and so, c is set to 1.Also, max is now 12. (In short, we will compare the elements with max. If height greater than max is obtained, then firstly we will change the max with new height and secondly, we will increase the counter by 1).

Next we compare 12(max) and 11. No changes had to be made.

Next we compare 12(max) and 14. Since, 12<14
Max will now be 14 and c will become 2.

SETTER’S SOLUTION:

#include<stdio.h>

int main()
{
    int t,max,c,x;
    long int m,i;
    scanf("%d",&t);
    while(t--)
    {   max=0;
        c=0;
        scanf("%ld",&m);

        for(i=0;i<m;i++)
        {
            scanf("%d",&x);
            if(max<x)
            {
                max=x;
                c++;
            }
        }
        printf("%d ",c-1);
    }
    return 0;
}