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.

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

Buddy, even though your real world experience gives you a meaning to things, you have to cover up your code from all the edge cases: you never know what kind of input your function take, especially if the problem description does not specify that the input array contains positive integers only, and a negative ints array is a valid input for our function

You are correct that you have to proof safe your code but the mandate is telling you what input you are getting. Positive integer arrays. Refer to the constraints section of the explanation.

But you shouldn't do anything like int max = 0 (except if there are no negative numbers or if it's indicating the index of the maximum value).
Instead you should use: int max = ar[0];

If you dont mind can you please provide a feedback on my video by commenting or liking & disliking. It will help me and others too to find the solution over internet.

If you dont mind can you please provide a feedback on my video by commenting or liking & disliking. It will help me and others too to find the solution over internet.

Count should be initialized to 1 as there is atleast one tallest. In this case, if the frequency of largest element is 0, count would return 0 which is an error

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 →

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

Can a birthday cake candle have a negative length?

No, candles cannot have a negative length buddy. I don't kniw what kind of world your living buddy.

Buddy, even though your real world experience gives you a meaning to things, you have to cover up your code from all the edge cases: you never know what kind of input your function take, especially if the problem description does not specify that the input array contains positive integers only, and a negative ints array is a valid input for our function

You are correct that you have to proof safe your code but the mandate is telling you what input you are getting. Positive integer arrays. Refer to the constraints section of the explanation.

But you shouldn't do anything like int max = 0 (except if there are no negative numbers or if it's indicating the index of the maximum value). Instead you should use: int max = ar[0];

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.

int max=ar[0]; this wrang staement , replace with int max= 0

exactly same i did but it give me error of indexoutofbound exception an can't resolve it" :( what to do?

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) {

Hi,

Your solution is good but it will take O(nlogn) time due to sorting which can be optimized to O(n)

Here is the video explanation of my solution in O(n) time -

https://youtu.be/1gxFE9EfanE

Any comments and feedback will be appreciated.

Thanks for that. I worked out a solution where I do it in O(n) time. Posted on your video.

most welcome. :)

If you dont mind can you please provide a feedback on my video by commenting or liking & disliking. It will help me and others too to find the solution over internet.

thanks

most welcome. :)

If you dont mind can you please provide a feedback on my video by commenting or liking & disliking. It will help me and others too to find the solution over internet.

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

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

Count should be initialized to 1 as there is atleast one tallest. In this case, if the frequency of largest element is 0, count would return 0 which is an error

Mine fails on that too. ar.sort(); ar.reverse(); let m = 0; let k = ar[0]; while (parseInt(ar[m]) === parseInt(k)){ m++; } return m; }

Hi,

Here is the video explanation of my solution in O(n) time -

https://youtu.be/1gxFE9EfanE

Any comments and feedback will be appreciated.

while (

i>-1&&a[i--] == tallest) { count++; }You just forgot to check whether the index i is within the bounds.

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

}

Thanks a lot!!.I was just wondering where did i go wrong..Thanks a lot again

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?

This code is assuming that first entered element is always the largest one.So it wont work in some cases... in short it is a wrong code.