Facing the sun | Practice | GeeksforGeeks

Given an array H representing heights of buildings. You have to count the buildings which will see the sunrise (Assume : Sun rise on the side of array starting point).

Example 1:
Input: 
N = 5
H[] = {7, 4, 8, 2, 9}
Output: 3
Explanation: As 7 is the first element, it
can see the sunrise. 4 can't see the
sunrise as 7 is hiding it.  8 can see.
2 can't see the sunrise. 9 also can see
the sunrise.

Example 2:
Input: 
N = 4
H[] = {2, 3, 4, 5}
Output: 4
Explanation: As 2 is the first element, it
can see the sunrise.  3 can see the
sunrise as 2 is not hiding it. Same for 4
and 5, they also can see the sunrise.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function countBuildings() which takes the array of integers h and n as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 106
1 ≤ Hi ≤ 108
 
0
harshilrpanchal19981 day ago
java solution
 
class Solution {   int countBuildings(int h[], int n) {       // code here       int ans = h[0];       int count =1;       for (int i=1;i < n ; i++){           if (h[i] > ans){               ans = h[i];               count++;           }       }       return count;   }}
0
swapniltayal4221 month ago
class Solution{public: // Returns count buildings that can see sunlightint countBuildings(int h[], int n) {    // code here    int maxh = 0;    int count = 0;    for (int i=0; i<n; i++){        if (h[i] > maxh){            maxh = h[i];            count++;        }    }return count;}};
-1
tyagis46061 month ago
C++ Solution :
 
int countBuildings(int h[], int n) {    // code here    int ans =0;    int count =0;        for(int i=0;i<n;i++){        if(h[i]>ans){            count++;            ans = max(ans,h[i]);        }    }    return count;}
0
torq242 months ago
Simple Python Code
 
def countBuildings(self,h, n):       c=h[0]       count=1       for i in range(1,n):           if h[i]>c:               count+=1               c=h[i]       return count
0
aakasshuit2 months ago
//Java Solution

        int max = 0;
        int count=0;
        for(int i=0;i<n;i++){
            if(h[i]>max){
                max=h[i];
                count++;
            }
        }
        return count;
0
mail2arman20012 months ago
int countBuildings(int h[], int n) {    // code here    int maxi=INT_MIN;    int count=0;    for(int i=0;i<n;i++)    {        if(h[i]>maxi)        {            maxi=h[i];            count++;        }    }    return count;}
0
ramdurgasais2 months ago
In Python
def countBuildings(self,h, n):
        top_building_height = buildings_can_see_sunrice = 0 
        
        for building_height in h:
            if top_building_height < building_height : 
                top_building_height = building_height
                buildings_can_see_sunrice += 1
                
        return buildings_can_see_sunrice
0
singhalyash813 months ago
// { Driver Code Starts#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends//User function template for C++class Solution{public: // Returns count buildings that can see sunlightint countBuildings(int h[], int n) {    // code here    int count=1;    int temp=h[0];    for(int i=1;i<n;i++)    {        if(h[i]>temp)        {            temp=h[i];            count+=1;        }    }    return count;}};
// { Driver Code Starts.
int main() {   int t;   cin >> t;   while (t--) {       int n;       cin >> n;       int h[n];       for (int i = 0; i < n; i++) {           cin >> h[i];       }       Solution ob;       auto ans = ob.countBuildings(h, n);       cout << ans << "\n";   }   return 0;} // } Driver Code Ends
+1
ritikakumari596733 months ago
solution inc++
 //take avariable intialise it with 0;and take one variable max intialise it with h[0]and everytime when u get element <then max update max with h[i]  element at that index and also increment it with 1. and at last return count.
// { Driver Code Starts#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends//User function template for C++class Solution{public: // Returns count buildings that can see sunlightint countBuildings(int h[], int n) {    // code here    int count=1;   int max=h[0];    for(int i=1;i<n;i++)    {    if(max<h[i])    {        count++;        max=h[i];    }    }    return count;}};
// { Driver Code Starts.
int main() {   int t;   cin >> t;   while (t--) {       int n;       cin >> n;       int h[n];       for (int i = 0; i < n; i++) {           cin >> h[i];       }       Solution ob;       auto ans = ob.countBuildings(h, n);       cout << ans << "\n";   }   return 0;} // } Driver Code Ends
0
indiakamanthan3 months ago
class Solution {
    int countBuildings(int h[], int n) {
        
        int max=0;
        int count=1;
        max=h[0];
        for(int i=1;i<n;i++)
        {
            if(max<h[i])
            {
                count++;
                max=h[i];
            }
            
        }
        return count;
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.