 # Count Buildings Hacker Earth coding question with solution

Here we will solve Hacker earth coding question asked in Renault off campus recruitment. question tittled as “count buildings” was asked as a part of coding challenge round

Question:

There is a cricket stadium and to the right of the stadium, there are N number of buildings in a straight line. Bob wants to see the cricket match from the top floor of the building, but there are some conditions to see the match from ith building. The conditions are as follows

• Bob can only see the match from the top floor of ith building if there exists at most K[i] buildings in front of bob with height less than and equal to the height of ith building.
• If any building is found in front of bob with height more than the height of ith building then bob cannot see the match from ith building.

You are given following information:

• An array A of N integers denoting the heights of each building
• An array K of N integers

Determine the total number of buildings from which Bob can see the cricket match.

Example1:

Input:

• N=3
• A=[2,1,3]
• K=[1,2,1]

Output:

1

Approach

• For building with height 2 that is A
• The number of buildings in front of it that have a height smaller than or equal to 2 is 0 which is less than K=1.
• Here, Bob can see the cricket match from this building
• For Building with height 1 that is A
• The number of buildings in front of it that has a height smaller than or equal to 1 is also 0 (zero) which is less than K=2,But since there exists a building with height 2 in front of it.
• Bob can not see cricket match from this building.
• For building with height 3 that is A
• The number of buildings in front of it that have a height smaller than or equal to 3 is 2 which is greater than K=1.
• Bob can not see cricket match from this building.

Therefore the final answer is 1

Example2:

Input:

N=5

A=[5,6,8,4,10]

K=[1,2,1,2,2]

Output:

2

Algorithm to Solve

1. Initially take input from user which represents number of buildings
2. Then take array of integers representing height of each element.
3. Next take K array elements.
4. Here we need to satisfy 2 conditions
• The builds infront of current building should have height less than or equal to height of current building.
• The next condition says that “number of buildings with height less than or equal to height of current building should be ” less than or equal to K[Current building position].
5. So take each element of the building height array and compare with the elements before it, if any building is found with height > current building height, then break loop as Bob canoot see cricket match from there.
6. If above condition is not satisfied the find total buildings count which satisfies 4th point and compare with corresponding K[i] element if it satisfies condition then Bob can see the match.
7. Repeat the step untill all builds inside array are compared.
8. Return an integer denoting total ways in which bob can see the cricket match.

JAVA CODE

```package arrayproblems;
import java.util.*;
class probem9 {
public static void main(String[] args) {
int N=5;
int [] array1= {5,6,8,4,10};
int [] array2= {1,2,1,2,2};
int res=0;
for(int i=0;i<array1.length;i++) {
int count=0;
boolean flag=false;
for(int j=0;j<i;j++) {

if(array1[j]<array1[i]) {
count++;
}
else {
flag=true;
}
}
if(count<array2[i] && flag==false) {
res=res+1;
}
}
System.out.println("no of ways in which bob can see match : " + res);
}
}
```

Watch complete explanation for Code

Similar off campus coding questions

Wipro Elite Coding Question Solved

Crack TCS Coding Round By solving These Questions

Tech Mahindra off Campus Coding Question

Complete Solution For Rob And Mars Problem In Java