Piling up ! Stucked !!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

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 ๐Ÿ˜‹

28th Mar 2020, 3:48 AM
Monkey D. Luffy
Monkey D. Luffy - avatar
4 Answers
+ 6
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
28th Mar 2020, 7:07 AM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 10
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.
28th Mar 2020, 5:41 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 9
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.
28th Mar 2020, 5:49 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Yes , but it must for all cases except when it has 1 element but that's not so
28th Mar 2020, 5:43 AM
Monkey D. Luffy
Monkey D. Luffy - avatar