Piling up ! Stucked !!!

Here is a problem i was solving https://www.hackerrank.com/challenges/piling-up/problem?h_r=internal-search Here's what i tried https://code.sololearn.com/cp9M8Fgi25kI/?ref=app I know there is some logical error in this , But can you help me figure out where 😋

3/28/2020 3:48:36 AM


4 Answers

New Answer


Gaurav Agrawal has already stated the problem. 👍 You are attempting to pop() elements from deque even after it's empty. For comparing two elements you are removing them. You could just use [ ] operator and access elements whithout removing them. Remove only that element which is greater from either side. Removing both first and last breaks the logic. You have put all this code at top level. It's generally preferred to break the logic withing functions/modules. Placing all the code within single function / top level makes it hard to debug. I couldn't debug your code so I started from beginning. I have wrote a function that gives next big elements from deque and also follows all those rules specified. This is complete code and it's working (I got my first hakerrank badge, lol) but don't copy paste it. Learn from code n comments and write code by your own hands. You can also develop little different logic based on this. https://code.sololearn.com/crLf50lw823z/?ref=app


I did't read up whole code but major problem is you are taking out 1 element from left and 1 from right without checking size of deque, if there is only 1 element in deque then it will give error so it will be better to take value of leftmost and rightmost element in some variable and then remove the value later from deque.


Aaditay try this test case having 2 elements. Probabaly that means it will give error in all test cases as at one point of time only 1 element will be left in deque but you will be removing two elements resulting in error. 1 2 4 3 I think line 29 should be deque size instead to get current no. of elements left in deque.


Yes , but it must for all cases except when it has 1 element but that's not so