Facing the sun
PROBLEM :
An array of buildings is facing the sun. The heights of the building is given in an array. You have to tell which all buildings will see the sunset.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the number of buildings.
The second line of each test case contains N input H[i],height of building.
Output:
Print the total number of buildings which will see the sunset.
Constraints:
1 = T = 100
1 = N = 500
1 = H[i] = 1000
Example:
Input:
2
5
7 4 8 2 9
4
2 3 4 5
Output:
3
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t,no,a[1000],i,top,count ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
count=1 ;
top=a[0] ;
for(i=0;i<no;i++)
{
if(a[i]>top)
{top=a[i];count++ ;}
}
cout<<count<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
An array of buildings is facing the sun. The heights of the building is given in an array. You have to tell which all buildings will see the sunset.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the number of buildings.
The second line of each test case contains N input H[i],height of building.
Output:
Print the total number of buildings which will see the sunset.
Constraints:
1 = T = 100
1 = N = 500
1 = H[i] = 1000
Example:
Input:
2
5
7 4 8 2 9
4
2 3 4 5
Output:
3
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t,no,a[1000],i,top,count ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>a[i] ;
count=1 ;
top=a[0] ;
for(i=0;i<no;i++)
{
if(a[i]>top)
{top=a[i];count++ ;}
}
cout<<count<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment