There is an array of buildings, with various heights, in a position. You have to find the ones that have view to the sea, because you want to see how many options for hotel reservations there are.
Sample Input
5
20 45 25 30 15
Sample Output
2
Only 2 have view to the sea because they are taller than other previous hotels.
So I thought that I would make a boolean that is false, except if there aren't any taller buildings before the i position in the array.
#include <iostream>
usingnamespace std;
int main() {
int ans=0;
bool view=false;
int n;
cin>>n;
int hotels[n];
for (int z=0; z<n; z++){
cin>>hotels[z];
}
for (int i=1; i<n; i++){
for (int j=0; j<i-1; j++){
if (hotels[i]>hotels[j])
view=true;
else
view=false;
}
if (view==true)
view=false;
ans++;
}
cout<<ans+1; //+1 because the 1st will always have view.
return 0;
}
1. take the rightmost hotel height and subtract it from all the others on the left
2. then take the rightmost positive height and subtract that from the others on the left
3. keep repeating until none left
any with a remaining height of zero are 'the ones'.
Try it. You don't have to write the program, just pencil and paper will do. And you can check whether I'm right. I must admit the comment starting with ' any with a ... ' is not exactly right, but the essence is there without being misleading.
Once you convince yourself you can then make an 'informed decision' whether you proceed to code it up or not. It shouldn't take more than about 5 minutes withe five hotels. 1 minute more likely. :)
I can't see why because going from the left hand end you only get 2 hotels as you show and I mentioned earlier which is why I queried it with the OP before, against the sample output.
So UYA
I followed the thread perfectly well. I fixed your output, which was working off of the original output, to work off of the edited output, which OP changed after his third post, yet you kept going off of anyway. Please stop with the insults. This place should be for safe discussion.
Stop arguing the point grub. You didn't follow the thread and tried to be smart with your sarcastic FTFY. If you want to use that sort of tone then expect a return on the same volume. You're the one making this place unsafe and unpleasant with your smart arse street talk and lack of education. Learn to read english!