Given an array representing heights of buildings. The array has buildings from left to right as shown in below diagram, count number of buildings facing the sunset. It is assumed that heights of all buildings are distinct.

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
Asked in : Amazon Interview
It can be easily observed that only the maximum element found so far will see the sunlight
i.e. curr_max will see the sunlight and then only the element greater than curr_max will see the sunlight. We traverse given array from left to right. We keep track of maximum element seen so far. Whenever an element becomes more than current max, increment result and update current max.
Implementation:
C++
#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 || arr[i] == curr_max) {
count++;
curr_max = arr[i];
}
}
return count;
}
int main()
{
int arr[] = { 7, 4, 8, 2, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countBuildings(arr, n);
return 0;
}
|
Java
class Test {
static 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 || arr[i] == curr_max) {
count++;
curr_max = arr[i];
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 7, 4, 8, 2, 9 };
System.out.println(countBuildings(arr, arr.length));
}
}
|
Python3
def countBuildings(arr, n):
count = 1
curr_max = arr[0]
for i in range(1, n):
if (arr[i] > curr_max or arr[i] == curr_max):
count += 1
curr_max = arr[i]
return count
arr = [7, 4, 8, 2, 9]
n = len(arr)
print(countBuildings(arr, n))
|
C#
using System;
class GFG {
static 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 || arr[i] == curr_max) {
count++;
curr_max = arr[i];
}
}
return count;
}
public static void Main()
{
int[] arr = { 7, 4, 8, 2, 9 };
Console.Write(countBuildings(arr, arr.Length));
}
}
|
PHP
<?php
function countBuildings($arr, $n)
{
$count = 1;
$curr_max = $arr[0];
for ( $i = 1; $i < $n; $i++)
{
if ($arr[$i] > $curr_max || $arr[$i] == $curr_max)
{
$count++;
$curr_max=$arr[$i];
}
}
return $count;
}
$arr = array(7, 4, 8, 2, 9);
$n = sizeof($arr) / sizeof($arr[0]);
echo countBuildings($arr, $n);
?>
|
Javascript
<script>
function countBuildings( arr, n)
{
let count = 1;
let curr_max = arr[0];
for (let i = 1; i < n; i++) {
if (arr[i] > curr_max || arr[i] == curr_max) {
count++;
curr_max = arr[i];
}
}
return count;
}
let arr = [ 7, 4, 8, 2, 9 ];
let n = arr.length;
document.write(countBuildings(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
14 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...