This Python program is designed to solve a problem of stacking cubes. The user inputs the number of test cases and for each case, the number of cubes and their lengths. The goal is to determine if it is possible to stack all the cubes in such a way that the length of each cube is less than or equal to the cube below it.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution uses a deque from the collections module to store the lengths of the cubes. It then checks the cubes at the ends of the deque, removing the larger one if it is less than or equal to the last cube placed on the stack. If it encounters a cube that is larger than the last one placed, it concludes that it is not possible to stack the cubes in the required manner. The program uses a loop to repeat this process for each test case.
from collections import deque
cas = int(input())
for _ in range(cas):
n = int(input())
dq = deque(map(int, input().split()))
possible = True
element = (2**31) + 1
while dq:
left_element = dq[0]
right_element = dq[-1]
if left_element >= right_element and element >= left_element:
element = dq.popleft()
elif right_element >= left_element and element >= right_element:
element = dq.pop()
else:
possible = False
break
if possible:
print("Yes")
else:
print("No")
If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.