Given the array arr[] of heights of certain buildings that lie adjacent to each other, sunlight starts falling from the left side of the buildings. If there is a building of a certain height, all the buildings to the right side of it having lesser heights cannot see the sun. Find the total number of buildings that receive sunlight.
Example:
Input: arr[] = [6, 2, 8, 4, 11, 13] Output: 4 Explanation: Only buildings of height 6, 8, 11 and 13 can see the sun, hence output is 4.
Input: arr[] = [2, 5, 1, 8, 3] Output: 3 Explanation: Only buildings of height 2, 5 and 8 can see the sun, hence output is 3.
[Naive Approach] Using Nested loop - O(n^2) Time and O(1) Space
The idea is to compare each building's height with all the buildings on its left side. If any building's height is greater than current building's height, then skip this building. Otherwise, increment the answer.
Dry run for arr[] = [6, 2, 8, 4, 11, 13]:
Initialize ans as answer = 1 since the first building always receives sunlight.
For i = 1, compare 2 with 6, a taller building 6 exists so ans remains 1.
For i = 2, compare 8 with 6 and 2, no taller building exists so ans becomes 2.
For i = 3, compare 4 with 6, 2 and 8, a taller building 6 exists so ans remains 2.
For i = 4, compare 11 with 6, 2, 8 and 4, no taller building exists so ans becomes 3.
For i = 5, compare 13 with 6, 2, 8, 4 and 11, no taller building exists so ans becomes 4.
Final answer is 4.
C++
#include<iostream>#include<vector>usingnamespacestd;intvisibleBuildings(vector<int>&arr){intans=0;for(inti=0;i<arr.size();i++){boolflag=true;// check the left sided buildingsfor(intj=0;j<i;j++){// if greater building found // mark the flag falseif(arr[j]>arr[i]){flag=false;break;}}if(flag){ans++;}}returnans;}intmain(){vector<int>arr={6,2,8,4,11,13};cout<<visibleBuildings(arr)<<endl;return0;}
Java
publicclassGfG{staticintvisibleBuildings(int[]arr){intans=0;for(inti=0;i<arr.length;i++){booleanflag=true;// check the left sided buildingsfor(intj=0;j<i;j++){// if greater building found // mark the flag falseif(arr[j]>arr[i]){flag=false;break;}}if(flag){ans++;}}returnans;}publicstaticvoidmain(String[]args){int[]arr={6,2,8,4,11,13};System.out.println(visibleBuildings(arr));}}
Python
defvisibleBuildings(arr):ans=0foriinrange(len(arr)):flag=True# check the left sided buildingsforjinrange(i):# if greater building found # mark the flag falseifarr[j]>arr[i]:flag=Falsebreakifflag:ans+=1returnansif__name__=="__main__":arr=[6,2,8,4,11,13]print(visibleBuildings(arr))
C#
usingSystem;classGfG{staticintvisibleBuildings(int[]arr){intans=0;for(inti=0;i<arr.Length;i++){boolflag=true;// check the left sided buildingsfor(intj=0;j<i;j++){// if greater building found mark the flag falseif(arr[j]>arr[i]){flag=false;break;}}if(flag){ans++;}}returnans;}staticvoidMain(){int[]arr={6,2,8,4,11,13};Console.WriteLine(visibleBuildings(arr));}}
JavaScript
functionvisibleBuildings(arr){letans=0;for(leti=0;i<arr.length;i++){letflag=true;// check the left sided buildingsfor(letj=0;j<i;j++){// if greater building found // mark the flag falseif(arr[j]>arr[i]){flag=false;break;}}if(flag){ans++;}}returnans;}// Driver codeletarr=[6,2,8,4,11,13];console.log(visibleBuildings(arr));
Output
4
[Expected Approach] Using Iterative Method - O(n) Time and O(1) Space
The idea is to iterate over the array from left to right and compare each building's height with the maximum height of a building so far. If its height is greater than maximum height, then increment the answer and update the value of maximum height.
Note: The first building will always receive sunlight as it does not have any building on its left side. So we can start iteration from second index and set maximum height to height of first building.
Dry run for arr[] = [6, 2, 8, 4, 11, 13]:
Initialize maxi as max height = 6 and ans as answer = 1.
For i = 1, 2 is smaller than maxi so ans remains 1.
For i = 2, 8 is greater than maxi so ans becomes 2 and maxi updates to 8.
For i = 3, 4 is smaller than maxi so ans remains 2.
For i = 4, 11 is greater than maxi so ans becomes 3 and maxi updates to 11.
For i = 1, 13 is greater than maxi so ans becomes 4 and maxi updates to 13.
Final answer is 4.
C++
#include<iostream>#include<vector>usingnamespacestd;intvisibleBuildings(vector<int>&arr){// Answer is set to one as first// building will get lightintans=1;// It will hold the value of maximum// height of a building.intmaxi=arr[0];for(inti=1;i<arr.size();i++){// If the current building has// the maximum height so farif(arr[i]>=maxi){// Increment the answerans++;// Update maximum valuemaxi=arr[i];}}returnans;}intmain(){vector<int>arr={6,2,8,4,11,13};cout<<visibleBuildings(arr)<<endl;return0;}
Java
classGfG{staticintvisibleBuildings(intarr[]){// Answer is set to one as first// building will get lightintans=1;// It will hold the value of maximum// height of a building.intmaxi=arr[0];for(inti=1;i<arr.length;i++){// If the current building has// the maximum height so farif(arr[i]>=maxi){// Increment the answerans++;// Update maximum valuemaxi=arr[i];}}returnans;}publicstaticvoidmain(String[]args){intarr[]={6,2,8,4,11,13};System.out.println(visibleBuildings(arr));}}
Python
defvisibleBuildings(arr):# Answer is set to one as first# building will get lightans=1# It will hold the value of maximum# height of a building.maxi=arr[0]foriinrange(1,len(arr)):# If the current building has# the maximum height so farifarr[i]>=maxi:# Increment the answerans+=1# Update maximum valuemaxi=arr[i]returnansif__name__=="__main__":arr=[6,2,8,4,11,13]print(visibleBuildings(arr))
C#
usingSystem;usingSystem.Collections.Generic;classGfG{staticintvisibleBuildings(int[]arr){// Answer is set to one as first// building will get lightintans=1;// It will hold the value of maximum// height of a building.intmaxi=arr[0];for(inti=1;i<arr.Length;i++){// If the current building has// the maximum height so farif(arr[i]>=maxi){// Increment the answerans++;// Update maximum valuemaxi=arr[i];}}returnans;}staticvoidMain(string[]args){int[]arr={6,2,8,4,11,13};Console.WriteLine(visibleBuildings(arr));}}
JavaScript
functionvisibleBuildings(arr){// Answer is set to one as first// building will get lightletans=1;// It will hold the value of maximum// height of a building.letmaxi=arr[0];for(leti=1;i<arr.length;i++){// If the current building has// the maximum height so farif(arr[i]>=maxi){// Increment the answerans++;// Update maximum valuemaxi=arr[i];}}returnans;}// Driver codeconstarr=[6,2,8,4,11,13];console.log(visibleBuildings(arr));