There are N buildings in a row with different heights(H[i]). You are viewing the buildings from the left and can see the roof of a building, if no building to the left of that building has height greater than equal to height of that building. You are asked the number of buildings whose roofs you can see.
Input Format
The first line contains N denoting number of buildings.
The next line contains N space separated integers denoting heights of the buildings from left to right.
Output Format
The output should contain one integer which is the number of buildings whose roofs you can see.
Example 1
Input
5 1 2 2 4 3
Output
3
Explanation
Going from left, we can see building at index 0, 1 and 3 was there is no building height greater or equal to them at their left.
Example 2
Input
5 1 2 3 4 5
Output
5
Explanation
Building at index 2 will hide before building at index 1 and building at index 4 will hide before building at index 3. In the second example, all buildings would be visible
Constraints
1 <= H[i] <= 10^15
Solution of Buildings in java:-
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { //your code here Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String [] arr=new String[n]; for(int i=0;i<n;i++) { arr[i]=sc.next(); } int count=1; String max=arr[0]; for(int i=1;i<n;i++) { if(Long.parseLong(max)<Long.parseLong(arr[i])) { count++; max=arr[i]; } } System.out.print(count); } }
Add a Comment