In this HackerRank Piling Up! problem solution There is a horizontal row of n cubes. The length of each cube is given. You need to create a new vertical pile of cubes. The new pile should follow these directions: if cube[i] is on top of cube[j] then sideLength[j] >= sideLength[i]. When stacking the cubes, you can only pick up either the leftmost or the rightmost cube each time. Print Yes if it is possible to stack the cubes. Otherwise, print No.

HackerRank Piling Up! in python problem solutionA

HackerRank Piling Up! in python problem solution

# Enter your code here. Read input from STDIN. Print output to STDOUT
N = int(raw_input())
for i in range(0,N):
    success = True
    c = int(raw_input())
    nums = map(int, raw_input().split())
    previous = float('inf')
    for j in range(0,c):
        if nums[0]>nums[len(nums)-1]:
            k = 0
            k = len(nums)-1
        pick = nums[k]
        del nums[k]
        if pick > previous:
            success = False
        previous = pick
    if success:
        print "Yes"
        print "No"