Number of buildings facing the sun

QUESTION

Given an array representing heights of buildings. The array has buildings from left to right , count number of buildings facing the sunset.\n\nExamples:\n\nInput : arr[ ] = {7, 4, 8, 2, 9}\nOutput: 3\nExplanation: As 7 is the first element, it can \nsee the sunset.\n4 can’t see the sunset as 7 is hiding it. \n8 can see.\n2 can’t see the sunset.\n9 also can see the sunset.\n\nInput : arr[ ] = {2, 3, 4, 5}\nOutput : 4.

“TESTCASE_1”: “5\n7 4 8 2 9\n###—###SEPERATOR—###—\n3”, “TESTCASE_2”: “4\n2 3 4 5\n###—###SEPERATOR—###—\n4”, “TESTCASE_3”: “6\n2 5 7 15 20 30\n###—###SEPERATOR—###—\n6”, “TESTCASE_4”: “7\n2 6 12 10 45 23 10\n###—###SEPERATOR—###—\n4”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
int main()
{
	int n,i,j,count=1;
  	scanf("%d",&n);
  	int a[n];
  	for(i=0;i<n;i++)
  		scanf("%d",&a[i]);
  	for(i=0;i<n-1;i++)
  	{
  		for(j=i+1;j<n;j++)
  		{
  			if(a[j]>a[i])
  			{
  				i=j;
  				count++;
			}
		}
	}
	printf("%d",count);
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.