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
Example
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))
Input
[4, 3, 7], [5, 9, 10], 11
Output
True