Number of Buildings Facing the Sun (original) (raw)

Last Updated : 16 Apr, 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. 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.

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 Yourselfredirect icon

Table of Content

[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]:

  1. Initialize ans as answer = 1 since the first building always receives sunlight.
  2. For i = 1, compare 2 with 6, a taller building 6 exists so ans remains 1.
  3. For i = 2, compare 8 with 6 and 2, no taller building exists so ans becomes 2.
  4. For i = 3, compare 4 with 6, 2 and 8, a taller building 6 exists so ans remains 2.
  5. For i = 4, compare 11 with 6, 2, 8 and 4, no taller building exists so ans becomes 3.
  6. 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 #include using namespace std;

int visibleBuildings(vector& arr) { int ans = 0;

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

    // check the left sided buildings
    for (int j = 0; j < i; j++) {

        // if greater building found 
        // mark the flag false
        if (arr[j] > arr[i]) {
            flag = false;
            break;
        }
    }

    if (flag) {
        ans++;
    }
}

return ans;

}

int main() {

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

return 0;

}

Java

public class GfG {

static int visibleBuildings(int[] arr) {
    int ans = 0;

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

        // check the left sided buildings
        for (int j = 0; j < i; j++) {

            // if greater building found 
            // mark the flag false
            if (arr[j] > arr[i]) {
                flag = false;
                break;
            }
        }

        if (flag) {
            ans++;
        }
    }

    return ans;
}

public static void main(String[] args) {

    int[] arr = {6, 2, 8, 4, 11, 13};
    System.out.println(visibleBuildings(arr));
}

}

Python

def visibleBuildings(arr): ans = 0

for i in range(len(arr)):
    flag = True

    # check the left sided buildings
    for j in range(i):

        # if greater building found 
        # mark the flag false
        if arr[j] > arr[i]:
            flag = False
            break

    if flag:
        ans += 1

return ans

if name == "main": arr = [6, 2, 8, 4, 11, 13] print(visibleBuildings(arr))

C#

using System;

class GfG {

static int visibleBuildings(int[] arr) {
    int ans = 0;

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

        // check the left sided buildings
        for (int j = 0; j < i; j++) {

            // if greater building found mark the flag false
            if (arr[j] > arr[i]) {
                flag = false;
                break;
            }
        }

        if (flag) {
            ans++;
        }
    }

    return ans;
}

static void Main() {

    int[] arr = {6, 2, 8, 4, 11, 13};
    Console.WriteLine(visibleBuildings(arr));
}

}

JavaScript

function visibleBuildings(arr) { let ans = 0;

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

    // check the left sided buildings
    for (let j = 0; j < i; j++) {

        // if greater building found 
        // mark the flag false
        if (arr[j] > arr[i]) {
            flag = false;
            break;
        }
    }

    if (flag) {
        ans++;
    }
}

return ans;

}

// Driver code let arr = [6, 2, 8, 4, 11, 13]; console.log(visibleBuildings(arr));

`

[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]:

  1. Initialize maxi as max height = 6 and ans as answer = 1.
  2. For i = 1, 2 is smaller than maxi so ans remains 1.
  3. For i = 2, 8 is greater than maxi so ans becomes 2 and maxi updates to 8.
  4. For i = 3, 4 is smaller than maxi so ans remains 2.
  5. For i = 4, 11 is greater than maxi so ans becomes 3 and maxi updates to 11.
  6. For i = 1, 13 is greater than maxi so ans becomes 4 and maxi updates to 13.

Final answer is 4.

C++ `

#include #include using namespace std;

int visibleBuildings(vector& arr) {

// 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 < arr.size(); 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 << visibleBuildings(arr) << endl;

return 0;

}

Java

class GfG { static int visibleBuildings(int arr[]) {

    // 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 < arr.length; 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(visibleBuildings(arr));
}

}

Python

def visibleBuildings(arr):

# 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, len(arr)):
    
    # 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(visibleBuildings(arr))

C#

using System; using System.Collections.Generic;

class GfG { static int visibleBuildings(int[] arr) {

    // 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 < arr.Length; 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(visibleBuildings(arr));
}

}

JavaScript

function visibleBuildings(arr) {

// 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 < arr.length; 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;

}

// Driver code const arr = [6, 2, 8, 4, 11, 13]; console.log(visibleBuildings(arr));

`