Find if neat arrangement of cups and shelves can be made in Python

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

The arrangement is considered neat if it follows these conditions:

  • No shelf can hold both cups and saucers

  • A shelf may contain at most 5 cups

  • A shelf may contain at most 10 saucers

Problem Understanding

Given arrays p = [4, 3, 7], q = [5, 9, 10], and m = 11, we need to determine if arrangement is possible.

Total cups = 4 + 3 + 7 = 14, requiring ?14/5? = 3 shelves
Total saucers = 5 + 9 + 10 = 24, requiring ?24/10? = 3 shelves
Total shelves needed = 3 + 3 = 6 ? 11, so the answer is True.

Algorithm

To solve this problem, we follow these steps:

  • Calculate total number of cups and saucers

  • Find minimum shelves needed for cups: ?total_cups / 5?

  • Find minimum shelves needed for saucers: ?total_saucers / 10?

  • Check if total required shelves ? available shelves

Implementation

import math

def is_valid(p, q, m):
    # Calculate total cups and saucers
    total_cups = sum(p)
    total_saucers = sum(q)
    
    # Calculate minimum shelves needed
    shelves_for_cups = math.ceil(total_cups / 5)
    shelves_for_saucers = math.ceil(total_saucers / 10)
    
    # Check if arrangement is possible
    total_shelves_needed = shelves_for_cups + shelves_for_saucers
    
    return total_shelves_needed <= m

# Test the function
p = [4, 3, 7]
q = [5, 9, 10]
m = 11

print(f"Cups: {p}, Total: {sum(p)}")
print(f"Saucers: {q}, Total: {sum(q)}")
print(f"Available shelves: {m}")
print(f"Can arrange neatly: {is_valid(p, q, m)}")
Cups: [4, 3, 7], Total: 14
Saucers: [5, 9, 10], Total: 24
Available shelves: 11
Can arrange neatly: True

Alternative Implementation

Using integer division to calculate ceiling without importing math module:

def is_valid_alternative(p, q, m):
    total_cups = sum(p)
    total_saucers = sum(q)
    
    # Calculate ceiling using integer division
    shelves_for_cups = (total_cups + 4) // 5  # Equivalent to ceil(total_cups/5)
    shelves_for_saucers = (total_saucers + 9) // 10  # Equivalent to ceil(total_saucers/10)
    
    return (shelves_for_cups + shelves_for_saucers) <= m

# Test with different examples
test_cases = [
    ([4, 3, 7], [5, 9, 10], 11),
    ([1, 2, 3], [4, 5, 6], 2),
    ([10, 15], [20, 30], 8)
]

for i, (cups, saucers, shelves) in enumerate(test_cases, 1):
    result = is_valid_alternative(cups, saucers, shelves)
    print(f"Test {i}: {result}")
Test 1: True
Test 2: False
Test 3: True

Key Points

  • Use ceiling function to calculate minimum shelves needed

  • Cups and saucers must be stored on separate shelves

  • The formula (n + k - 1) // k calculates ceiling division without importing math

Conclusion

This problem requires calculating minimum shelves needed for cups and saucers separately, then checking if the total doesn't exceed available shelves. The key insight is using ceiling division to handle cases where items don't divide evenly into shelf capacity.

Updated on: 2026-03-25T09:40:22+05:30

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements