You are viewing a single comment's thread. Return to all comments →
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;