• + 0 comments
    1. Sort cubes list as an expected stack to be find
    2. Check if picking only left or right cubes is possible to pile the expected stack
    T = int(input())
    
    def is_stackable(cubes):
        expected_stack = sorted(cubes)
        
        left, right = 0, len(cubes) - 1
        while len(expected_stack) > 0:
            expected_block = expected_stack.pop()
            
            if cubes[left] == expected_block:
                left += 1
            elif cubes[right] == expected_block:
                right -= 1
            else:
                return False
            
        return True
    
    for t in range(T):
        n = int(input())
        cubes = list(map(int, input().split()))
        print("Yes" if is_stackable(cubes) else "No")