Sunday, 2 April 2017

Hacker Rank solution for Birthday Cake Candles

Problem:- Hacker Rank Solution Program In C++ For " Birthday Cake Candles " or Hacker Rank Solution Program In C++ For Birthday Cake Candles or Birthday Cake Candles solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for Birthday Cake Candles subdomain or Birthday Cake Candles solution in c++ of hacker rank.

Check This- Hacker rank Solution for C++ Sub Domain Introduction, Classes, STL, Inheritance.

Logic:- Birthday Cake Candles is very simple I am going to explain this problem in simple steps. So basically in this problem, first we have to find the largest number than 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 problem Birthday Cake Candles.

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 sorting technique like Bubble sort, Insertion sort, Selection sort

Sorting of an Array

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

Now the next step is to compare how many elements in the array is 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 than respectfully increase the counter by 1.
  
temp=array[n-1];
   
for(i=0; i<n; i++)
{
        if(temp==array[i])
            count++;
}

Last step is to print the counter this is the answer of the problem Birthday Cake Candles

cout<<count;

Hence we get our answer but remember in this solution in a comment some statement is written these statement for checking is our sorting is working fine or not.

Also check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen c++ editor in the top right drop down option.

Submit your solution here:- Click here

Tip:- Before copy the solution I recommended please read this full article, this will help you to build your own logic. 

If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.


Solution:-

#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;
}

Output:-

Hacker Rank Solution For " Birthday Cake Candles " in C++

You May Like This:-










10. C Program For Find A Grade Of Given Marks Using Switch Case

No comments:
Write comments

Recommended Posts × +