Buildings receiving sunlight

PROBLEM :

Given are the heights of certain Buildings which lie adjacent to each other. Sunlight starts falling from left side of the buildings. If there is a building of certain Height, all the buildings to the right side of it having lesser heights cannot see the sun . The task is to find the total number of such buildings that receive sunlight.



Input:
First Line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. First line of each test case contains an integer N denoting the number of buildings. Second line contains N space separated integers H1, H2,...HN denoting heights of buildings.

Output:
Corresponding to each test case, print the desired output in a new line

Constraints:
1<=T<=100
1<=N<=100
1<=Hi<=100

Example:
Input:
3
6
6 2 8 4 11 13
5
2 5 1 8 3
7
3 4 1 0 6 2 3

Output:
4
3
3

Explanation:
In the first example only buildings of height 6 8 11 and 13 can see the sun, hence output is 4.

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int Buildings_receiving_sunlight(int [],int ) ;
int main()
 {
int t,no,arr[100],i ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
     
   no=Buildings_receiving_sunlight(arr,no) ;
   cout<<no<<endl ;
}
return 0;
}

int Buildings_receiving_sunlight(int arr[],int no)
{
    int i,max,count ;
    max=arr[0] ;
    count=1 ;
   
    for(i=1;i<no;i++)
    {
        if(max<=arr[i])
        {
            max=arr[i] ;
            count++ ;
        }
    }
    return count ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )