Skip to content

Commit

Permalink
Merge pull request #69 from gaursakshi/patch-1
Browse files Browse the repository at this point in the history
 Birthday Cake Candle.cpp
  • Loading branch information
maze-runnar committed Dec 26, 2019
2 parents ee8fa90 + c35b938 commit 924ddc1
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -12,6 +12,7 @@ in C++, Java and Python.
||[Counting Valleys](https://www.hackerrank.com/challenges/counting-valleys/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup)|[Done](/Warm-Up%20Challenges/counting_valleys.cpp)|[Done](/Warm-Up%20Challenges/countingValleys.java)|[Done](/Warm-Up%20Challenges/CountingValleys.py)|
||[Jumping on the Clouds](https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup)|[Done](/Warm-Up%20Challenges/Jumping_on_the_clouds.cpp)|[Done](/Warm-Up%20Challenges/jumpingOnTheClouds.java)|[Done](/Warm-Up%20Challenges/JumpingOnTheClouds.py)|
||[Repeated Strings](https://www.hackerrank.com/challenges/repeated-string/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup)|[Done](/Warm-Up%20Challenges/repeated_string.cpp)|[Done](/Warm-Up%20Challenges/repeatedString.java)|[Done](/Warm-Up%20Challenges/RepeatedStrings.py)|
||[Birthday Cake Candles](https://www.hackerrank.com/challenges/birthday-cake-candles/problem)|[Done](/Warm-Up%20Challenges/Birthday%20Cake%20Candle.cpp)|||
|Arrays | | | | |
||[Arrays: Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Arrays/array_left_rotation.cpp)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Arrays/LeftRotation.java)|[Done](/Arrays/LeftRotation.py)|
||[New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Arrays/new_year_chaos.cpp)|[Done](https://github.com/maze-runnar/interview-preparation-kit/blob/master/Arrays/NewYearChaos.java)|[Done](/Arrays/NewYearChaos.py)|
Expand Down
116 changes: 116 additions & 0 deletions Warm-Up Challenges/Birthday Cake Candle.cpp
@@ -0,0 +1,116 @@
/*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 .
Input
4
3 2 1 3
Output Format
2
Return the number of candles that can be blown out on a new line.*/

#include <bits/stdc++.h>

using namespace std;

vector<string> split_string(string);

// Complete the birthdayCakeCandles function below.
int birthdayCakeCandles(vector<int> ar)
{
int largest=ar[0];
int count=0;

for(int i=1;i<ar.size();i++)
{
if(ar[i]>largest)
{
largest=ar[i];
}
}

for(int i=0;i<ar.size();i++)
{
if(ar[i]==largest)
{
count++;
}
}

return count;


}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

int ar_count;
cin >> ar_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

string ar_temp_temp;
getline(cin, ar_temp_temp);

vector<string> ar_temp = split_string(ar_temp_temp);

vector<int> ar(ar_count);

for (int i = 0; i < ar_count; i++) {
int ar_item = stoi(ar_temp[i]);

ar[i] = ar_item;
}

int result = birthdayCakeCandles(ar);

fout << result << "\n";

fout.close();

return 0;
}

vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});

input_string.erase(new_end, input_string.end());

while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}

vector<string> splits;
char delimiter = ' ';

size_t i = 0;
size_t pos = input_string.find(delimiter);

while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));

i = pos + 1;
pos = input_string.find(delimiter, i);
}

splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

return splits;
}

0 comments on commit 924ddc1

Please sign in to comment.