-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from gaursakshi/patch-1
Birthday Cake Candle.cpp
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |