Birthday Cake Candles Hackerrank Solution in C++ Programming
Birthday cake candles hackerrank solution in C++ programming.
In this article, you will make the birthday cake candles hackerrank solution in C++ programming.
Example
Enter the size of the array::
5
Enter the 5 elements of the array::
2 5 1 3 5
TALLEST CANDLES:: 2
You should have knowledge of the following topics in c++ programming to understand these programs:
- C++
main()
function - C++
while
loop - C++ Data Type
Source Code
// Birthday Cake Candles Hackerrank Solution in C++ Programming
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int s=0;
cout << "Enter the size of the array::\n";
cin >> s;
int candles[s];
cout << "\nEnter the " << s << " elements of the array::\n";
int i=0;
while (i<s) {
cin >> candles[i];
i++;
}
// It will calculate the tallest candles from the given array
double max=-INFINITY;
int l=s, counter=0;
while (l--) {
if (candles[l] > max) {
max=candles[l];
}
}
l=s;
while (l--) {
if (candles[l] == max) {
counter++;
}
}
// It will print the final output of the program
cout << "\nTALLEST CANDLES:: " << counter;
return 0;
}
Output
Enter the size of the array::
5
Enter the 5 elements of the array::
2 5 1 3 5
TALLEST CANDLES:: 2
Explanation
In this given program, we have taken the input size of the array 5
and elements of the array 2 5 1 3 5
. After that, we made iterations on each element of the array to count the highest value from the array and set a counter variable counter to count the highest values.
Then It will return the counting of the tallest candles of the given array is 2
.