Check if it is possible to survive on Island in Python

Suppose there is an island with only one store that remains open every day except Sunday. We need to determine if someone can survive for a given number of days and find the minimum number of days they need to buy food.

Given the following parameters:

  • N - Maximum number of food units someone can buy each day
  • S - Number of days someone is required to survive
  • M - Number of food units required each day to survive

Starting from Monday, we need to check if survival is possible for the next S days and calculate the minimum buying days required.

Algorithm Logic

The solution follows these key steps:

  • Check if survival is impossible: when weekly food capacity is insufficient or daily requirement exceeds daily purchase limit
  • Calculate minimum buying days needed based on total food requirement
  • Account for partial purchases by rounding up the division

Example

Let's implement the island survival checker:

def solve(S, N, M):
    # Check if survival is impossible
    if ((N * 6) < (M * 7) and S > 6) or M > N:
        return False
    else:
        # Calculate minimum days to buy food
        count = (M * S) // N
        if ((M * S) % N) != 0:
            count += 1
        return (True, count)

# Test with given parameters
S = 12  # Survive for 12 days
N = 24  # Can buy 24 food units per day
M = 3   # Need 3 food units per day

result = solve(S, N, M)
print(f"Can survive: {result[0]}, Minimum buying days: {result[1]}")
Can survive: True, Minimum buying days: 2

How It Works

For the example with S=12, N=24, M=3:

  • Total food needed: 12 × 3 = 36 units
  • Can buy 24 units per day, so need ?36/24? = 2 buying days
  • Strategy: Buy 24 units on day 1 (lasts 8 days), then buy 12 more units for remaining 4 days

Edge Cases

Let's test some edge cases:

def solve(S, N, M):
    if ((N * 6) < (M * 7) and S > 6) or M > N:
        return False
    else:
        count = (M * S) // N
        if ((M * S) % N) != 0:
            count += 1
        return (True, count)

# Test cases
test_cases = [
    (5, 10, 2),   # Short survival period
    (10, 5, 6),   # Daily need exceeds buying capacity
    (15, 20, 18)  # Weekly capacity insufficient
]

for S, N, M in test_cases:
    result = solve(S, N, M)
    print(f"S={S}, N={N}, M={M} ? {result}")
S=5, N=10, M=2 ? (True, 1)
S=10, N=5, M=6 ? False
S=15, N=20, M=18 ? False

Conclusion

This algorithm efficiently determines island survival feasibility by checking weekly food capacity constraints and calculating minimum purchase days. The key insight is that survival fails when daily requirements exceed purchase limits or weekly capacity is insufficient for extended periods.

Updated on: 2026-03-25T15:14:08+05:30

610 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements