COMA02 - Editorial

PROBLEM LINK

Practice

Contest

Author : Hatim Tai

Tester : Deepanshu Bhatia

Editorialist : Hatim Tai

DIFFICULTY :

Simple

PREREQUISITES:

Linear Search

PROBLEM :

Given a number O and an array A having N numbers, If the first element of the array is less than O output ‘-1’ otherwise find the count of numbers which are greater than or equal to O.and this count can go upto M.

EXPLANATION :

To find the count of numbers greater than or equal to O first initialise cnt=0 and simply traverse the whole array, compare each number if it is greater than or equal to O ? If it is, increment ‘cnt’ by ‘1’.

Finally see if this ‘cnt’ is greater than M or not ? If it is then set cnt = M, since the value of ‘cnt’ can go upto M.

cnt = 0
if(A[0] < O) 
    cnt = -1
else
{
    for(i = 0; i < N ; i++)
    {
        if(A[i] >= cnt)
            cnt = cnt + 1
    }
}
if(cnt != -1) 
    cnt = min(cnt,M)
print cnt