Number of buildings facing the sun

Last Updated : 17 Jan, 2026

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. The task is to find the total number of buildings that receive sunlight.

Examples: 

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.

1


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.

22
Try it on GfG Practice
redirect icon

[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 continue. Otherwise, increment the answer.

C++
// C++ Program to find number of
// buildings facing sun
#include <bits/stdc++.h>
using namespace std;

int longest(vector<int>& arr) {
    int n = arr.size();

    // base case
    if (n == 0)
        return 0;

    // Answer is set to one as first
    // building will get light
    int ans = 0;

    for (int i = 0; i < n; i++) {
        bool maxi = true;

        // For each building, check all
        // the buildings on its left side.
        for (int j = 0; j < i; j++) {

            // If a building has greater height
            // than current building, then set
            // maxi = false.
            if (arr[j] > arr[i]) {
                maxi = false;
                break;
            }
        }

        // If the current building's height
        // is greater than all buildings on
        // left side, then increment answer.
        if (maxi) {
            ans++;
        }
    }

    return ans;
}

int main() {

    vector<int> arr = {6, 2, 8, 4, 11, 13};
    cout << longest(arr) << endl;

    return 0;
}
C
// C Program to find number of 
// buildings facing sun
#include <stdio.h>

int longest(int arr[], int n) {
    
    // base case
    if (n == 0) return 0;
    
    // Answer is set to zero
    int ans = 0;
    
    for (int i = 0; i < n; i++) {
        int maxi = 1;
        
        // For each building, check all 
        // the buildings on its left side.
        for (int j = 0; j < i; j++) {
            
            // If a building has greater height 
            // than current building, then set
            // maxi = false.
            if (arr[j] > arr[i]) {
                maxi = 0;
                break;
            }
        }
        
        // If the current building's height
        // is greater than all buildings on
        // left side, then increment answer.
        if (maxi) {
            ans++;
        }
    }
    
    return ans;
}

int main() {
  
    int arr[] = {6, 2, 8, 4, 11, 13};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", longest(arr, n));
    return 0;
}
Java
// Java Program to find number of 
// buildings facing sun
class GfG {

    static int longest(int[] arr) {
        int n = arr.length;
        
        // base case
        if (n == 0) return 0;
        
        // Answer is set to zero
        int ans = 0;
        
        for (int i = 0; i < n; i++) {
            boolean maxi = true;
            
            // For each building, check all 
            // the buildings on its left side.
            for (int j = 0; j < i; j++) {
                
                // If a building has greater height 
                // than current building, then set
                // maxi = false.
                if (arr[j] > arr[i]) {
                    maxi = false;
                    break;
                }
            }
            
            // If the current building's height
            // is greater than all buildings on
            // left side, then increment answer.
            if (maxi) {
                ans++;
            }
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {6, 2, 8, 4, 11, 13};
        System.out.println(longest(arr));
    }
}
Python
# Python Program to find number of 
# buildings facing sun

def longest(arr):
    n = len(arr)
    
    # base case
    if n == 0:
        return 0
    
    # Answer is set to zero
    ans = 0
    
    for i in range(n):
        maxi = True
        
        # For each building, check all 
        # the buildings on its left side.
        for j in range(i):
            
            # If a building has greater height 
            # than current building, then set
            # maxi = False.
            if arr[j] > arr[i]:
                maxi = False
                break
        
        # If the current building's height
        # is greater than all buildings on
        # left side, then increment answer.
        if maxi:
            ans += 1
    
    return ans

if __name__ == "__main__":
    arr = [6, 2, 8, 4, 11, 13]
    print(longest(arr))
C#
// C# Program to find number of 
// buildings facing sun
using System;

class GfG {

    static int Longest(int[] arr) {
        int n = arr.Length;
        
        // base case
        if (n == 0) return 0;
        
        int ans = 0;
        
        for (int i = 0; i < n; i++) {
            bool maxi = true;
            
            // For each building, check all 
            // the buildings on its left side.
            for (int j = 0; j < i; j++) {
                
                // If a building has greater height 
                // than current building, then set
                // maxi = false.
                if (arr[j] > arr[i]) {
                    maxi = false;
                    break;
                }
            }
            
            // If the current building's height
            // is greater than all buildings on
            // left side, then increment answer.
            if (maxi) {
                ans++;
            }
        }
        
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {6, 2, 8, 4, 11, 13};
        Console.WriteLine(Longest(arr));
    }
}
JavaScript
// JavaScript Program to find number of 
// buildings facing sun

function longest(arr) {
    let n = arr.length;
    
    // base case
    if (n === 0) return 0;
    
    let ans = 0;
    
    for (let i = 0; i < n; i++) {
        let maxi = true;
        
        // For each building, check all 
        // the buildings on its left side.
        for (let j = 0; j < i; j++) {
            
            // If a building has greater height 
            // than current building, then set
            // maxi = false.
            if (arr[j] > arr[i]) {
                maxi = false;
                break;
            }
        }
        
        // If the current building's height
        // is greater than all buildings on
        // left side, then increment answer.
        if (maxi) {
            ans++;
        }
    }
    
    return ans;
}

let arr = [6, 2, 8, 4, 11, 13];
console.log(longest(arr));

Output
4

[Expected Approach - 1] Using Stack - O(n) time and O(n) space

We use a stack to keep track of buildings that can see the sunlight. As we iterate through the array, for each building, we pop the stack if any building in the stack is shorter or equal to the current building. The stack ultimately stores only the buildings that can receive sunlight.

C++
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

int longest(const vector<int>& arr) {
     stack<int> s;
    int count = 0;

    for (int i = 0; i < arr.size(); ++i) {

        // Remove all smaller or equal buildings on the left
        while (!s.empty() && s.top() <= arr[i]) {
            s.pop();
        }

        // If stack is empty, no taller building exists on the left
        if (s.empty()) {
            count++;
        }

        // Push current building height
        s.push(arr[i]);
    }

    return count;
}

int main() {
    vector<int> arr = {6, 2, 8, 4, 11, 13};
    cout << longest(arr) << endl;
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

int longest(int* arr, int size) {
    int* stack = (int*)malloc(size * sizeof(int));
    int top = -1;
    int count = 0;

    for (int i = 0; i < size; ++i) {

        // Remove all smaller or equal buildings on the left
        while (top != -1 && stack[top] <= arr[i]) {
            top--;
        }

        // If stack is empty, no taller building exists on the left
        if (top == -1) {
            count++;
        }

        // Push current building height
        stack[++top] = arr[i];
    }

    free(stack);
    return count;
}

int main() {
    int arr[] = {6, 2, 8, 4, 11, 13};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", longest(arr, size));
    return 0;
}
Java
import java.util.Stack;

public class GfG {

    public static int longest(int[] arr) {
        Stack<Integer> s = new Stack<>();
        int count = 0;

        for (int i = 0; i < arr.length; ++i) {

            // Remove all smaller or equal buildings on the left
            while (!s.isEmpty() && s.peek() <= arr[i]) {
                s.pop();
            }

            // If stack is empty, no taller building exists on the left
            if (s.isEmpty()) {
                count++;
            }

            // Push current building height
            s.push(arr[i]);
        }
        return count;
    }

    public static void main(String[] args) {
        int[] arr = {6, 2, 8, 4, 11, 13};
        System.out.println(longest(arr));
    }
}
Python
def longest(arr):
    stack = []
    count = 0

    for i in range(len(arr)):

        # Remove all smaller or equal buildings on the left
        while stack and stack[-1] <= arr[i]:
            stack.pop()

        # If stack is empty, no taller building exists on the left
        if not stack:
            count += 1

        # Push current building height
        stack.append(arr[i])

    return count


arr = [6, 2, 8, 4, 11, 13]
print(longest(arr))
C#
using System;
using System.Collections.Generic;

class GfG {

    static int Longest(int[] arr) {
        Stack<int> s = new Stack<int>();
        int count = 0;

        for (int i = 0; i < arr.Length; ++i) {

            // Remove all smaller or equal buildings on the left
            while (s.Count > 0 && s.Peek() <= arr[i]) {
                s.Pop();
            }

            // If stack is empty, no taller building exists on the left
            if (s.Count == 0) {
                count++;
            }

            // Push current building height
            s.Push(arr[i]);
        }

        return count;
    }

    static void Main() {
        int[] arr = { 6, 2, 8, 4, 11, 13 };
        Console.WriteLine(Longest(arr));
    }
}
JavaScript
function longest(arr) {
    let stack = [];
    let count = 0;

    for (let i = 0; i < arr.length; ++i) {

        // Remove all smaller or equal buildings on the left
        while (stack.length > 0 && stack[stack.length - 1] <= arr[i]) {
            stack.pop();
        }

        // If stack is empty, no taller building exists on the left
        if (stack.length === 0) {
            count++;
        }

        // Push current building height
        stack.push(arr[i]);
    }

    return count;
}

const arr = [6, 2, 8, 4, 11, 13];
console.log(longest(arr));

Output
4

[Expected Approach - 2] 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.

C++
// C++ Program to find number of 
// buildings facing sun
#include <bits/stdc++.h>
using namespace std;

int longest(vector<int>& arr) {
    int n = arr.size();
    
    // base case
    if (n == 0) return 0;
    
    // Answer is set to one as first
    // building will get light
    int ans = 1;
    
    // It will hold the value of maximum
    // height of a building.
    int maxi = arr[0];
    
    for (int i = 1; i < n ; i++) {
        
        // If the current building has
        // the maximum height so far
        if (arr[i] >= maxi) {
            
            // Increment the answer
            ans++;
            
            // Update maximum value
            maxi = arr[i];
        }
    }
    
    return ans;
}

int main() {
    
    vector<int> arr = {6, 2, 8, 4, 11, 13};
    
    cout << longest(arr) << endl;
    
    return 0;
}
C
// C Program to find number of 
// buildings facing sun
#include <stdio.h>

// Function to find the number of buildings facing sun
int longest(int arr[], int n) {
    
    // base case
    if (n == 0) return 0;
    
    // Answer is set to one as first
    // building will get light
    int ans = 1;
    
    // It will hold the value of maximum
    // height of a building.
    int maxi = arr[0];
    
    for (int i = 1; i < n; i++) {
        
        // If the current building has
        // the maximum height so far
        if (arr[i] >= maxi) {
            
            // Increment the answer
            ans++;
            
            // Update maximum value
            maxi = arr[i];
        }
    }
    
    return ans;
}

int main() {
    
    int arr[] = {6, 2, 8, 4, 11, 13};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    printf("%d\n", longest(arr, n));
    
    return 0;
}
Java
// Java Program to find number of 
// buildings facing sun
import java.util.*;

class GfG {
    static int longest(int arr[]) {
        int n = arr.length;
        
        // base case
        if (n == 0) return 0;
        
        // Answer is set to one as first
        // building will get light
        int ans = 1;
        
        // It will hold the value of maximum
        // height of a building.
        int maxi = arr[0];
        
        for (int i = 1; i < n; i++) {
            
            // If the current building has
            // the maximum height so far
            if (arr[i] >= maxi) {
                
                // Increment the answer
                ans++;
                
                // Update maximum value
                maxi = arr[i];
            }
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int arr[] = {6, 2, 8, 4, 11, 13};
        
        System.out.println(longest(arr));
    }
}
Python
# Python Program to find number of 
# buildings facing sun

def longest(arr):
    n = len(arr)
    
    # base case
    if n == 0:
        return 0
    
    # Answer is set to one as first
    # building will get light
    ans = 1
    
    # It will hold the value of maximum
    # height of a building.
    maxi = arr[0]
    
    for i in range(1, n):
        
        # If the current building has
        # the maximum height so far
        if arr[i] >= maxi:
            
            # Increment the answer
            ans += 1
            
            # Update maximum value
            maxi = arr[i]
    
    return ans

if __name__ == "__main__":
    arr = [6, 2, 8, 4, 11, 13]
    
    print(longest(arr))
C#
// C# Program to find number of 
// buildings facing sun
using System;
using System.Collections.Generic;

class GfG {
    static int Longest(int[] arr) {
        int n = arr.Length;
        
        // base case
        if (n == 0) return 0;
        
        // Answer is set to one as first
        // building will get light
        int ans = 1;
        
        // It will hold the value of maximum
        // height of a building.
        int maxi = arr[0];
        
        for (int i = 1; i < n; i++) {
            
            // If the current building has
            // the maximum height so far
            if (arr[i] >= maxi) {
                
                // Increment the answer
                ans++;
                
                // Update maximum value
                maxi = arr[i];
            }
        }
        
        return ans;
    }

    static void Main(string[] args) {
        int[] arr = { 6, 2, 8, 4, 11, 13 };
        
        Console.WriteLine(Longest(arr));
    }
}
JavaScript
// JavaScript Program to find number of 
// buildings facing sun

function longest(arr) {
    const n = arr.length;
    
    // base case
    if (n === 0) return 0;
    
    // Answer is set to one as first
    // building will get light
    let ans = 1;
    
    // It will hold the value of maximum
    // height of a building.
    let maxi = arr[0];
    
    for (let i = 1; i < n; i++) {
        
        // If the current building has
        // the maximum height so far
        if (arr[i] >= maxi) {
            
            // Increment the answer
            ans++;
            
            // Update maximum value
            maxi = arr[i];
        }
    }
    
    return ans;
}

const arr = [6, 2, 8, 4, 11, 13];
console.log(longest(arr));

Output
4


Comment