Birthday Cake Candles -Hackerrank Problem solving
PROBLEM STATEMENT:
You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she will only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.
EXPLANATION:
For example, if your niece is turning 4 years old, and the cake will have 4 candles of height 4 4 1,3 she will be able to blow out 2 candles successfully, since the tallest candles are of height 4 and there are 2 such candles.
HOW TO SOLVE:
Here we need to find maximum number from array, print number of times the maximum number occurs in that array.
PROGRAM:
#include
int main()
{
int n,arr[100],i,max=-999,c=0;
scanf("%d",&n);
for(i=0;i
{
scanf("%d",&arr[i]);//take input array
}
for(i=n-1;i>=0;i--)
{
if(arr[i]>max)//find maximum number from array
max=arr[i];
}
for(i=0;i
{
if(max = = arr[i])//if maximum is equal to array element then count is incremented
c+=1;
}
printf("%d",c);// print count
return 0;
}
PYTHON IMPLEMENTATION
n=int(input())
a=list(map(int,input().split())
k=max(a)
print(a.count(k))
Python uses it's built in methods to find maximum,count from array.
This reduces overhead of for loops which we use in c.
HOPE THIS HELPS..............
Happy coding.............