- Related Questions & Answers
- Find if neat arrangement of cups and shelves can be made in C++
- Checking if a string can be made palindrome in JavaScript
- Is pizza healthy? If not, can it be made healthy?
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Check if a two-character string can be made using given words in Python
- Check if array sum can be made K by three operations on it in Python
- Program to find the length of the longest, that can be made using given letters in Python
- How can Indian prisons be made secure?
- Filling Bookcase Shelves in Python
- Can a constructor be made final in Java?
- Check whether the number can be made perfect square after adding 1 in Python
- Find if n can be written as product of k numbers in C++
- Count of numbers which can be made power of 2 by given operation in C++
- How can predictions be made on Auto MPG dataset using TensorFlow?
- Find if array can be divided into two subarrays of equal sum in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have three different types of cups in array p and saucers in array q and m number of shelves, we have to check whether a neat arrangement of cups and shelves can be made.

We can say that arrangement of the cups and saucers will be neat if it follows these conditions − 1. No shelf can hold both cups and saucers. 2. A self may contain at most 5 cups. 3. A self may contain at most 10 saucers.

So, if the input is like p = [4, 3, 7] q = [5, 9, 10] m = 11, then the output will be True as total number of cups = 14, 3 shelves are required, total saucers = 24, 3 shelves are required. So, total required shelves = 3 + 3 = 6, which is smaller than given number of shelves m.

To solve this, we will follow these steps −

sum_p := 0, sum_q := 0

for i in range 0 to size of p, do

sum_p := sum_p + p[i]

for i in range 0 to size of q, do

sum_q := sum_q + q[i]

m_p :=(sum_p + 4) / 5

m_q :=(sum_q + 9) / 10

if m_p + m_q <= m, then

return True

otherwise,

return False

Let us see the following implementation to get better understanding −

def is_valid(p, q, m): sum_p = 0 sum_q = 0 for i in range(0, len(p)): sum_p += p[i] for i in range(0,len(q)): sum_q += q[i] m_p = (sum_p + 5 - 1) / 5 m_q = (sum_q + 10 - 1) / 10 if(m_p + m_q <= m): return True else: return False p = [4, 3, 7] q = [5, 9, 10] m = 11 print(is_valid(p ,q ,m))

[4, 3, 7], [5, 9, 10], 11

True

Advertisements