Sunday, November 18, 2018

Birthday Cake Candles - Hacker Rank Solution

Birthday Cake Candles  - Hacker Rank Solution


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’ll
only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.
For example, if your niece is turning  years old, and the cake will have  candles of height , she will be able to blow out  candles successfully, since the tallest candles are of height  and there are  such candles.
Function Description
Complete the function birthdayCakeCandles in the editor below. It must return an integer representing the number of candles she can blow out.
birthdayCakeCandles has the following parameter(s):
  • ar: an array of integers representing candle heights
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 that can be blown 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 . Your niece only blows out the tallest candles, meaning the candles where . Because there are  such candles, we print  on a new line.
Birthday Cake Candles  - SOLUTION




There are two steps to solving this challenge:
  1. Find the maximum height, , of any candle.
  2. Count the number of occurrences of candles having  among all the candles, then print this number.
Problem Setter's code:

Python 2

n = input()
arr = map(int, raw_input().split())
print arr.count(max(arr))

Ruby

n = gets.strip.to_i
height = gets.strip
height = height.split(' ').map(&:to_i)
ans = 0
max_height = height.max
(0..n-1).each do |i|
    if height[i] == max_height
        ans = ans + 1
    end
end

print ans
Problem Tester's code:

Java

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        // the number of candles
        int n = scan.nextInt();
        
        // store the current maximum height of any candle, initialize to the minimum possible height of any candle
        int maxHeight = 1; 
        
        // count the number of candles having the maximum height
        int countMax = 0;
        
        for(int i = 0; i < n; i++) {
            int tmp = scan.nextInt();
            
            // if you read in a value larger than maxHeight, set new maxHeight
            if(tmp > maxHeight) {
                maxHeight = tmp;
                countMax = 1;
            }
            // if you read a value equal to the current value of maxHeight
            else if(tmp == maxHeight) {
             // increment the count of candles having maximum height
                countMax++;
            }
        }
        scan.close();
        
        System.out.println(countMax);
    }
}

Birthday Cake Candles  - Hacker Rank Solution

1 comment:

  1. #!/bin/python3

    import math
    import os
    import random
    import re
    import sys

    # Complete the 'birthdayCakeCandles' function below.
    # The function is expected to return an INTEGER.
    # The function accepts INTEGER_ARRAY candles as parameter.

    def birthdayCakeCandles(candles):
    # Write your code here
    max_cand = max(candles)
    total = 0
    for i in range(len(candles)):
    if candles[i] == max_cand:
    total+=1
    return total

    if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    candles_count = int(input().strip())
    candles = list(map(int, input().rstrip().split()))
    result = birthdayCakeCandles(candles)
    fptr.write(str(result) + '\n')
    fptr.close()

    ReplyDelete

Powered by Blogger.