5

this is my first time posting on StackOverflow, I apologize if my post format isn't standard and please forgive me if my question is stupid.

I'm doing challenges on hackerrank and the current objectives of my problem are as follows:

You have an empty sequence, and you will be given N queries. Each query is one of these three types:

  1. Push the element x onto the stack.
  2. Delete the element present at the top of the stack.
  3. Print the maximum element in the stack.

I've already solved it, but I saw another solution in the discussions and thought of modifying it. The alternate solution was:

public class Solution {

public static void main(String[] args) {

    Scanner inp = new Scanner(System.in);
    int n = inp.nextInt();
    Stack<Integer> S = new Stack<>();
    Stack<Integer> largest_stack = new Stack<>();
    largest_stack.push(0);

    for(int i=1; i<=n;i++) {
        int query = inp.nextInt();

        if(query==1){
            int newtop=inp.nextInt();
            S.push(newtop);

            if(S.peek()>=largest_stack.peek()){
                largest_stack.push(S.peek());     
            }
       }

        if(query==2) {
            if(S.peek()==largest_stack.peek()){
                largest_stack.pop();   
            }
            S.pop();   
        }
        if(query==3){
            System.out.println(largest_stack.peek());
        }
    }
}
}

I thought of replacing S.peek() with newtop in this part

            if(S.peek()>=largest_stack.peek()){
                largest_stack.push(S.peek());     
            }

So I get

            if(S.peek()>=largest_stack.peek()){
                largest_stack.push(newtop);     
            }

Upon doing so, it's passes the default test case. But it fails 9/27 total test cases. Here is a small portion of one of the test cases which is failing. There are 100000 queries but I've taken only the first 21.

21
1 809288903
3
1 55967040
1 967650885
1 21752006
3
2
1 61250743
1 975612397
3
3
2
3
2
3
2
2
3
3
2
1 784642399

Why is it failing? Any help would be much appreciated.

EDIT:

If I input the values I've posted above with the non modified code, I get

809288903
967650885
975612397
975612397
967650885
967650885
809288903
809288903

With modified code:

809288903
967650885
975612397
975612397
975612397
975612397
975612397
975612397
Axel
  • 13,939
  • 5
  • 50
  • 79
FV389
  • 81
  • 5
  • 3
    are you sure that this is the only change you've made? `newTop` and `S.peek()` are exactly the same at this point in time. – Thomas Jungblut Jan 25 '17 at 08:55
  • Yes, that is the only change I've made. Absolutely no other changes. – FV389 Jan 25 '17 at 08:56
  • 6
    The only thing I can imagine is that because you are pushing a new Integer object by autoboxing, thus the comparision in `if(S.peek()==largest_stack.peek()){` fails, as this should be actually using `equals` instead of the `==`. So you can try to replace the equality comparision with a call to `equals` and see whether it fixes it. – Thomas Jungblut Jan 25 '17 at 09:02
  • 2
    @ThomasJungblut this should be an answer. – default locale Jan 25 '17 at 09:06
  • @ThomasJungblut good point, you should write an answer explaining what's happening under the hood with these generic Integer collections. Maybe some words about caching of Integer values between -128 and 127. – ttzn Jan 25 '17 at 09:07
  • @coffeeninja I'll write something up later. Someone else can feel free to actually do it in the meantime. – Thomas Jungblut Jan 25 '17 at 09:09
  • Thank you Thomas, you were correct. It's working now. I have no knowledge of Java whatsoever, can you briefly explain why this was happening? – FV389 Jan 25 '17 at 09:14
  • 1
    @FV389 hey, sorry I can't add an answer anymore, but the linked question explains why. Basically there is a difference between primitive (int, long, boolean) and their object types (Integer, Long, Boolean). This entails that equality comparisions are carried out differently (methods vs. operators). Topics you may want to read up on are: Auto/Un-boxing and the above question/answer on in the equality comparision in the context of Strings. – Thomas Jungblut Jan 25 '17 at 17:10
  • Thanks for replying Thomas, I'll read up on them. – FV389 Jan 27 '17 at 05:53

0 Answers0