Given an array of integers, consider the elements as the height of the building, find the number of buildings that can be seen from the right side.
Examples:
Input : height[] = {2, 6, 2, 4, 0, 1}
Output : 3
we can see only 3 building i.e with height 1, 4 and 6.
Input : height[] = {4, 8, 2, 0, 0, 5}
Output : 2
This problem seems to be finding longest increasing sub sequence from right but actually it is not.We have to just increase the count if we encounter any building with greater height found so far.
Below is the implementation of above approach.
C++
// CPP program to find number of elements // that can be seen from right side. #include <bits/stdc++.h> using namespace std; int numberOfElements(int height[], int n) { int max_so_far = 0; int coun = 0; for (int i = n - 1; i >= 0; i--) { if (height[i] > max_so_far) { max_so_far = height[i]; coun++; } } return coun; } // Driver code int main() { int n = 6; int height[] = { 4, 8, 2, 0, 0, 5 }; cout << numberOfElements(height, n); return 0; } |
Java
// Java program to find number of elements // that can be seen from right side. import java.util.*; class Solution { static int numberOfElements(int height[], int n) { int max_so_far = 0; int coun = 0; for (int i = n - 1; i >= 0; i--) { if (height[i] > max_so_far) { max_so_far = height[i]; coun++; } } return coun; } // Driver code public static void main(String args[]) { int n = 6; int height[] = { 4, 8, 2, 0, 0, 5 }; System.out.println( numberOfElements(height, n)); } } //contributed by Arnab Kundu |
Python3
# Python3 program to find # number of elements # that can be seen from right side def numberOfElements(height, n): max_so_far = 0 coun = 0 for i in range(n-1,-1,-1): if height[i] > max_so_far: max_so_far = height[i] coun = coun + 1 return coun #Driver code if __name__=='__main__': n = 6 height = [4, 8, 2, 0, 0, 5] print(numberOfElements(height, n)) # This code is contributed by # Shashank_Sharma |
C#
// C# program to find number of elements // that can be seen from right side. using System; class GFG { public static int numberOfElements(int []height, int n) { int max_so_far = 0; int coun = 0; for (int i = n - 1; i >= 0; i--) { if (height[i] > max_so_far) { max_so_far = height[i]; coun++; } } return coun; } // Driver code public static void Main() { int n = 6; int []height = { 4, 8, 2, 0, 0, 5 }; Console.WriteLine(numberOfElements(height, n)); } } // This code is contributed by Soumik |
PHP
<?php // PHP program to find number of elements // that can be seen from right side. function numberOfElements($height, $n) { $max_so_far = 0; $coun = 0; for ($i = $n - 1; $i >= 0; $i--) { if ($height[$i] > $max_so_far) { $max_so_far = $height[$i]; $coun++; } } return $coun; } // Driver code $n = 6; $height = array(4, 8, 2, 0, 0, 5 ); echo numberOfElements($height, $n); // This code is contributed // by Akanksha Rai |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Number of Larger Elements on right side in a string
- Count the number of elements which are greater than any of element on right side of an array
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Count smaller elements on right side
- Remove minimum elements from either side such that 2*min becomes more than max
- Count smaller elements on right side using Set in C++ STL
- Replace every element of array with sum of elements on its right side
- Puzzle | Number of Sheets to be turned so that Prime Number has a Vowel on the other side
- Minimum swaps to group similar characters side by side?
- Count of smaller elements on right side of each element in an Array using Merge sort
- Find the farthest smaller number in the right side
- How to place two div side-by-side of the same height using CSS?
- Minimum number of points to be removed to get remaining points on one side of axis
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Count number of elements between two given elements in array
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Maximum number of contiguous array elements with same number of set bits
- Largest number dividing maximum number of elements in the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

