We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

int main() {
int n,ar_i;
scanf("%i", &n);
int *ar = malloc(sizeof(int) * n);
for(ar_i = 0; ar_i < n; ar_i++){
scanf("%i",&ar[ar_i]);
}
int result = birthdayCakeCandles(n, n, ar);
printf("%d\n", result);
return 0;
}
BRO THIS CODE SAYS TIME LIMIT EXCEEDED PLEASE HELP

intbirthdayCakeCandles(intn,vector<int>ar){// Complete this functionintmax=ar[0];intcount=0;for(inti=0;i<n;i++)if(ar[i]>max)max=ar[i];for(inti=0;i<n;i++)if(ar[i]==max)count++;returncount;}

just an optimization, even though in this case your code is fine:
if you try an array with negatives, then you ll never get inside your if-statements. so the numMaxHeight will never change, so you dont really compute it in such cases.
so this solution for an array with negatives, where the max negative appears more than once, will fail. eg [-1, -1]

proposed solution: set numMaxHeight = 0 on initialization

if you use quicksort u already have O(nlogn) which is worse than just iteratring through the array and saving the highest value and then just iterate a second time incrementing a counter everytime the value is similar to the saaved one. then complexity is O(2n) = O(n).
With n = 100 you would have ~ 600 steps(n*logn = 100 * 6 = 600) with your solution and just 200 steps with mine

Did you try using c++ std::sort(ar.begin(), ar.end()); ? I did and it worked flawlessly.
int birthdayCakeCandles(int n, vector<int> ar) {
// Complete this function
sort(ar.begin(), ar.end());
int max = ar[ar.size() -1];
int count = 0;
for(auto num : ar) {
if(num == max) ++count;
}
return count;

## Birthday Cake Candles

You are viewing a single comment's thread. Return to all comments →

Like this a lot. Thanks.

`c

## include

## include

## include

## include

## include

## include

## include

int birthdayCakeCandles(int n, int ar_size, int* ar) { // Complete this function int i,j,temp,count=0,b; for(i=0;i

return count; }

int main() { int n,ar_i; scanf("%i", &n); int *ar = malloc(sizeof(int) * n); for(ar_i = 0; ar_i < n; ar_i++){ scanf("%i",&ar[ar_i]); } int result = birthdayCakeCandles(n, n, ar); printf("%d\n", result); return 0; } BRO THIS CODE SAYS TIME LIMIT EXCEEDED PLEASE HELP

use c++ and you'll be fine

this code was working on some test case but not all please help me

/*You should do like this... */

In java, maybe this way can help you! :)

just an optimization, even though in this case your code is fine: if you try an array with negatives, then you ll never get inside your if-statements. so the numMaxHeight will never change, so you dont really compute it in such cases. so this solution for an array with negatives, where the max negative appears more than once, will fail. eg [-1, -1]

proposed solution: set numMaxHeight = 0 on initialization

You can simplify it like this:

int max=ar[0]; int count=0; for(int i=0;imax){ max=ar[i]; count = 1; //Reinitialize count if you found a new max }

} return count;

I this case you are traversing the vector only once.

i did the same thing.But it is showing me 4 test cases failed.

Be aware of time complexity. In my testcase 4, n was 10000. If you use a sorting algorithm with n^2, you will get a runtime error.

The way I structured my code is:

I tried this

It is failing the test case

100000 followed by 100000 times 9999999 (as height of candles(array values)).

Pass all Testcases..

Try This

static int birthdayCakeCandles(int[] ar) {

need to add one more check in while condition for all the array elements with same value

while ( i > -1 && a[i--] == tallest ) { count++; }

if you use quicksort u already have O(nlogn) which is worse than just iteratring through the array and saving the highest value and then just iterate a second time incrementing a counter everytime the value is similar to the saaved one. then complexity is O(2n) = O(n). With n = 100 you would have ~ 600 steps(n*logn = 100 * 6 = 600) with your solution and just 200 steps with mine

}

ar[i]-max==0 in second if worked for me

sandeep did u use unsigned long or any other data type which allows you to take in large amounts of data?

Since height of the candle is always at least 1, you can safely assume max = 0; Then loop all at once, you dont need a double loop. (Java syntax)

You don't need the second loop. Checking of equal can be done in the else of

`if(ar[i] > max)`

thank you!

Ask yourself a few questions:

-The number of highest candles

-I need to know what the highest candle is.

-I need to know how many of them there are.

-Simple comparison should work for knowing what the highest is. Just store it in a var and reset when I find one that's bigger.

-Store the quantity in a var, increment it whenever I find one of the Highest candles and reset it each time I find a new highest candle.