AR12
QUESTION DESCRIPTION
Ramar has a plan to play a game with his friends. so he has an array representing heights of towers.
The array has towers from left to right , count number of towers facing the sunset.
Examples:
Input : arr[] = {7, 4, 8, 2, 9}
Output: 3
Explanation:
As 7 is the first element, it can see the sunset.
4 can't see the sunset as 7 is hiding it.
8 can see.
2 can't see the sunset.
9 also can see the sunset.
Input : arr[] = {2, 3, 4, 5}
Output : 4
Ramar has a plan to play a game with his friends. so he has an array representing heights of towers.
The array has towers from left to right , count number of towers facing the sunset.
Examples:
Input : arr[] = {7, 4, 8, 2, 9}
Output: 3
Explanation:
As 7 is the first element, it can see the sunset.
4 can't see the sunset as 7 is hiding it.
8 can see.
2 can't see the sunset.
9 also can see the sunset.
Input : arr[] = {2, 3, 4, 5}
Output : 4
TEST CASE 2
INPUT
INPUT
4
2 3 4 5
OUTPUT4
#include <iostream>
using namespace std;
int countBuildings(int arr[], int n)
{
int count = 1;
int curr_max = arr[0];
for (int i=1; i<n; i++)
{
if (arr[i] > curr_max)
{
count++;
curr_max=arr[i];
}
}
return count;
}
int main()
{ int n;
cin>>n;
int a[n],i;
for(i=0;i<n;i++)
cin>>a[i];
cout << countBuildings(a, n);
return 0;
}
Comments
Post a Comment