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.
Your problem(s) is that you push 0 on the max stack when you can't guarantee that the first incoming input will be less than 0. Use .MIN_VALUE instead.
Second, when you use max.peek() it will give EmptyStack exception if the max stack is empty (maybe some cases delete all the data there).You should first use max.empty() method to check if the stack is not empty then proceed.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
Your problem(s) is that you push 0 on the max stack when you can't guarantee that the first incoming input will be less than 0. Use .MIN_VALUE instead.
Second, when you use max.peek() it will give EmptyStack exception if the max stack is empty (maybe some cases delete all the data there).You should first use max.empty() method to check if the stack is not empty then proceed.