Birthday Cake Candles — Hacker Rank ( C, C++, Java, C#, Javascript, Ruby …)

You are in charge of the cake for a child’s birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest.

Example

Candles = [4,4,1,3]

The maximum height candles are 4 units high. There are 2 of them, so return 2.

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):

• int candles[n]: the candle heights

Returns

• int: the number of candles that are tallest

Input Format

The first line contains a single integer, n , the size of candles[].
The second line contains n space-separated integers, where each integer i describes the height of candles[i] .

Output Format

Return 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

Candle heights are[3,2,1,3] . The tallest candles are 3 units, and there are of 2 of them.

// C
int birthdayCakeCandles(int candles_count, int* candles) {
int count = 0;
int max = 0;
for(int i = 0; i < candles_count ; i++)
{
int num = candles[i];
if(num > max)
{
max = num;
count = 1;
}else if(max == num)
{
count++;
}
}
return count;
}
// C++
int birthdayCakeCandles(vector<int> candles) {
int count = 0;
int max = 0;
for(size_t i = 0; i < candles.size() ; i++)
{
int num = candles[i];
if(num > max)
{
max = num;
count = 1;
}else if(max == num)
{
count++;
}
}
return count;
}
// Java
public static int birthdayCakeCandles(List<Integer> candles) {
int count = 0;
int max = 0;
for(int i = 0; i < candles.size() ; i++)
{
int num = candles.get(i);
if(num > max)
{
max = num;
count = 1;
}else if(max == num)
{
count++;
}
}
return count;
}
// Csharp
public static int birthdayCakeCandles(List<int> candles)
{
int count = 0;
int max = 0;
foreach (var candle in candles)
{
int num = candle;
if(num > max)
{
max = num;
count = 1;
}else if(max == num)
{
count++;
}
}
return count;
}
// Javascript
function birthdayCakeCandles(candles) {
let count = 0;
let max = 0;
for(let i = 0; i < candles.length ; i++)
{
let num = candles[i];
if(num > max)
{
max = num;
count = 1;
}else if(max == num)
{
count++;
}
}
return count;
}
// Ruby
def birthdayCakeCandles(candles)

count = 0;
max = 0;
candles.each do |candle|
num = candle;
if (num > max)
max = num;
count = 1;
elsif (max == num)
count = count +1;
end
end
count;
end

student at Flatiron School