Friday, 28 April 2017

Hacker Rank Birthday Cake Candles Problem Solution.

Problem:
Colleen is turning  years old! She has  candles of various heights on her cake, and candle  has height . Because the taller candles tower over the shorter ones, Colleen can only blow out the tallest candles.
Given the  for each individual candle, find and print the number of candles she can successfully blow out.
Input Format
The first line contains a single integer, , denoting the number of candles on the cake.
The second line contains  space-separated integers, where each integer  describes the height of candle .
Constraints
Output Format
Print the number of candles Colleen blows out on a new line.
Sample Input 0
4
3 2 1 3
Sample Output 0
2
Explanation 0
We have one candle of height , one candle of height , and two candles of height . Colleen only blows out the tallest candles, meaning the candles where . Because there are  such candles, we print  on a new line.

Solution:


#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){ int a,n,height = -1,res = 0; vector<int> ar; cin >> n; for(int i = 0; i < n; i++){ cin >> a; if(a >= height)ar.push_back(height = a); } for(int i = 0; i < ar.size(); i++){ if(ar[i] == height)res++; } cout << res << endl; }

3 comments:

  1. #include
    #include
    #include
    #include
    #include
    #include
    #include


    int main(){
    int i;
    int n;
    int max=0;
    scanf("%d",&n);

    int a[n];
    int count=0;

    for(i=1;i<=n;i++){
    scanf("%d",&a[i]);
    if(max<a[i])
    max=a[i];
    }
    for(i=1;i<=n;i++)
    if(a[i]==max)
    count++;

    printf("%d",count);
    return 0;
    }

    ReplyDelete
  2. i have also same logic but due to time out source code was not accepted ...

    ReplyDelete