- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to check whether we can pile up cubes or not
Suppose we have an array nums containing size of n different cubes, they are placed horizontally. We have to make a pile of cubes vertically. The new cube should follow −
- if ith cube is on top of jth cube, then side length of jth one must be greater or equal to side length of ith one.
When we are making the vertical pile, we can only take cubes from left side or right side but not from the middle. We have to check whether we can pile them up or not.
So, if the input is like nums = [1,2,3,7,8], then the output will be True because we can take boxes from right to left to pile them up successfully.
To solve this, we will follow these steps −
- n := size of nums
- d := make a double ended queue from the elements of nums
- flag := True
- prev := 0
- while d is not empty, do
- first := d[0]
- last := d[n-1]
- if prev is not same as 0 and (first > prev or last > prev) , then
- flag := False
- come out from the loop
- if first >= last, then
- prev := left item of d, and delete it from d
- otherwise,
- prev := last item of d and delete it from d
- if flag is true, then
- return True
- otherwise,
- return False
Example
Let us see the following implementation to get better understanding
from collections import deque def solve(nums): n = len(nums) d = deque(nums) flag = True prev = 0 while d: first = d[0] last = d[-1] if prev != 0 and (first > prev or last > prev): flag = False break if first >= last: prev = d.popleft() else: prev = d.pop() if flag: return True else: return False nums = [1,2,3,7,8] print(solve(nums))
Input
[1,2,3,7,8]
Output
True
- Related Articles
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether we can get N queens solution or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to check whether Amal can win stone game or not in Python
- Program to check whether all can get a seat or not in Python
- Program to check whether we can make group of two partitions with equal sum or not in Python?
- Program to check we can reach leftmost or rightmost position or not in Python
- Program to check we can cross river by stones or not in Python
- Program to check we can form array from pieces or not in Python
- Program to check whether one point can be converted to another or not in Python
- Program to check whether we can pick up and drop every passenger in given list in Python
- Program to check whether one string swap can make strings equal or not using Python

Advertisements