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];

this code and your logic so complex, try to optimize it, you can sort this vector and count the element(s) had duplicate with the last( if you sort it greater).

## Birthday Cake Candles

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

/*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?

this code and your logic so complex, try to optimize it, you can sort this vector and count the element(s) had duplicate with the last( if you sort it greater).

I did like this as well but still it is not passing many test cases are not

Its the same thing

its not working