Here is a problem i was solving
Here's what i tried
I know there is some logical error in this , But can you help me figure out where 😋
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.
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.
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.
I think line 29 should be deque size instead to get current no. of elements left in deque.