29/01/2023

Birthday Cake Candles Hackerrank Solution in C | C++ Bonus

Find out the Birthday Cake Candles Hackerrank Solution in C and C++ programming languages. Here I am going to explain this problem in simple steps. So basically in this problem, first we have to find the largest number then we compare how many numbers are greater or equal to our largest number that's it. If you don't get the solution don't worry below is the step-by-step explanation of the Birthday Cake Candles Solution in C and C++ languages.

Birthday Cake Candles Hackerrank Solution in C

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. Below is the example after that we are going to find the Birthday Cake Candles Hackerrank Solution in C.

Birthday Cake Candles Hackerrank


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 candles successfully 2 since the tallest candles are of height 4 and there are 2 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, n, denoting the number of candles on the cake. The second line contains n space-separated integers, where each integer i describes the height of candle I. The input format is very useful to get Birthday Cake Candles Solution in C language.

Constraints

  • 1<=n<=10^5
  • 1<=ar[i]<=10^7

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

Explanation of Birthday Cake Candles Hackerrank Solution in C and C++ programming languages are below, this is the only explanation of the above input. We have one candle of height 1, one candle of height 2, and two candles of height 3. Your niece only blows out the tallest candles, meaning the candles where height = 3. Because there are 2 such candles, we print 2 on a new line. Let's move and try to understand the Birthday Cake Candles Code in C programming language.

Birthday Cake Candles Solution Explanation


First, we have to sort the array for sorting we are using vector array and sort function for minimizing the time or we can do the same thing by using a sorting technique like the Bubble sort, Insertion sort, Selection sort

Sorting of an Array


sort(array.begin(),array.end()); //an array start address index to end address index

Before moving to find the Birthday Cake Candles Solution in C, Now the next step is to compare how many elements in the array are equal to the largest element in an array. Before that put the largest element of an array to the temp variable and if the elements of an array are equal to the largest element of an array then respectfully increase the counter by 1. temp=array[n-1];


temp=array[n-1];
for(i=0; i<n; i++)
{
        if(temp==array[i])
            count++;
}

The last step is to print the counter this is the answer to the Birthday Cake Candles Code in C and C++. 


cout<<count;

Read: Hackerrank 30 Days of Code Solutions in C Language

Submit your solution here:-Click here

Tip: Before copying the solution I recommended please read this full article on the hackerrank website. Our aim is not to provide a solution, we want to help and motivate you so you can write your own code. let's come to the point and check the Birthday Cake Candles Solution in C.

Birthday Cake Candles Hackerrank Solution in C


#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*Birthday Cake Candles Code in C*/
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;
}

Above we have a Birthday Cake Candles Code in C programming now it's time to find a solution in C++ programming language.

The Output Birthday Cake Candles Hackerrank Solution in C


The Output Birthday Cake Candles Hackerrank Solution in C

Must Check: Hackerrank Solutions C++

Birthday Cake Candles Hackerrank Solution C++


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() 
{
    int n,temp=0,count=0,i;
    cin>>n;
    vector<int> array(n);
    for(i=0; i<n;i++)
    {
     cin>>array[i];
}
    sort(array.begin(),array.end());
temp=array[n-1];
/*for(int i=0; i<n;i++)
        cout<<c[i];
*/
    for(i=0; i<n; i++)
{
        if(temp==array[i])
            count++;
    }
cout<<count;
return 0;
} 

The Output of Birthday Cake Candles Hackerrank Solution C++


The Output of Birthday Cake Candles Hackerrank Solution C++

Similar to Birthday Cake Candles Hackerrank


Below are the 5 likely programming challenges in the Hackerrank Algorithm section take a look and try to understand the problem and solve it. If you have a better solution let us know as we have answered the above Birthday Cake Candles Hackerrank Solution in C.
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: