BackmediumArraysuncategorizedmedium

Mars Colony Resource Allocation Solution

Problem Statement

A Mars colony's life support system relies on a finite stockpile of essential resources to sustain its population. You are provided with two arrays of equal length, stock and consumption. The stock array contains the total available units for each resource type, while the consumption array specifies the exact units of each resource required to sustain a single inhabitant for one standard cycle.

Your task is to determine the maximum number of inhabitants that can be fully supported for exactly one cycle. An inhabitant is considered supported only if the colony has sufficient stock for every single resource type required by that individual. If the stock of any resource is insufficient to meet the per-inhabitant requirement, that resource becomes the bottleneck, limiting the total population that can be sustained.

Return the maximum integer count of inhabitants that can be supported. If no inhabitant can be supported due to zero stock in any required resource, return 0.

Example 1
Input
stock = [10, 20, 30], consumption = [2, 5, 10]
Output
5

Explanation: Calculate the maximum inhabitants supported by each resource individually: 1. Oxygen: 10 available / 2 required = 5 inhabitants. 2. Water: 20 available / 5 required = 4 inhabitants. 3. Food: 30 available / 10 required = 3 inhabitants. The limiting factor is Food, which supports only 3 inhabitants. Wait, let's re-calculate. 30/10 is 3. 20/5 is 4. 10/2 is 5. The minimum is 3. Let me correct the example values to ensure the output is 5 as stated in the draft or adjust the output. Let's use stock=[10, 25, 30], consumption=[2, 5, 10]. 1. 10/2 = 5. 2. 25/5 = 5. 3. 30/10 = 3. Min is 3. Let's try stock=[10, 20, 50], consumption=[2, 5, 10]. 1. 10/2 = 5. 2. 20/5 = 4. 3. 50/10 = 5. Min is 4. Let's try stock=[10, 25, 50], consumption=[2, 5, 10]. 1. 10/2 = 5. 2. 25/5 = 5. 3. 50/10 = 5. Min is 5. This works. Corrected Walkthrough: 1. Resource 1: 10 available / 2 required = 5. 2. Resource 2: 25 available / 5 required = 5. 3. Resource 3: 50 available / 10 required = 5. The minimum of these values is 5. Thus, 5 inhabitants can be supported.

Example 2
Input
stock = [100, 100, 100], consumption = [1, 2, 3]
Output
33

Explanation: Calculate the capacity for each resource: 1. Resource 1: 100 / 1 = 100. 2. Resource 2: 100 / 2 = 50. 3. Resource 3: 100 / 3 = 33 (integer division). The bottleneck is Resource 3, which limits the colony to 33 inhabitants.

Example 3
Input
stock = [5, 0, 10], consumption = [1, 1, 1]
Output
0

Explanation: Calculate the capacity for each resource: 1. Resource 1: 5 / 1 = 5. 2. Resource 2: 0 / 1 = 0. 3. Resource 3: 10 / 1 = 10. The minimum value is 0. Since the stock of Resource 2 is zero, no inhabitants can be supported.

Constraints

  • 1 <= stock.length <= 10^5
  • stock.length == consumption.length
  • 0 <= stock[i] <= 10^9
  • 1 <= consumption[i] <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Mars Colony Resource Allocation — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

A Mars colony's life support system relies on a finite stockpile of essential resources to sustain its population. You are provided with two arrays of equal length, stock and consumption. The stock array contains the total available units for each resource type, while the consumption array specifies the exact units of each resource required to sustain a single inhabitant for one standard cycle.

Your task is to determine the maximum number of inhabitants that can be fully supported for exactly one cycle. An inhabitant is considered supported only if the colony has sufficient stock for every single resource type required by that individual. If the stock of any resource is insufficient to meet the per-inhabitant requirement, that resource becomes the bottleneck, limiting the total population that can be sustained.

Return the maximum integer count of inhabitants that can be supported. If no inhabitant can be supported due to zero stock in any required resource, return 0.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Mars Colony Resource Allocation"

medium

WHY DOES IT MATTER?

Identifying the limiting resource is essential because it transforms a potentially exponential search into a linear scan. This pattern is common in capacity planning, supply chain optimization, and real‑time resource scheduling.

OPTIMIZATION CHALLENGE

The key insight is that the maximum sustainable inhabitants equals the minimum of the per‑resource quotients. Recognizing this reduces the problem from a combinatorial search to a simple reduction operation.

REAL-WORLD CONNECTION

In distributed systems, this mirrors the concept of *bottleneck* identification: the throughput of a pipeline is governed by its slowest stage. Similarly, the colony’s population is bounded by the scarcest life‑support resource.

During interviews, emphasize the mathematical reasoning: explain that each resource independently caps the population, so the overall cap is the minimum of those caps. This demonstrates both algorithmic insight and clear communication.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đź’ľ Space:O(1)

Core Theory — Why This Approach?

The core of the Mars Colony Resource Allocation problem is a classic example of a *resource‑bounded* optimization. Each resource type has a finite stockpile (stock[i]) and a fixed per‑inhabitant consumption (consumption[i]). The maximum number of inhabitants that can be sustained is limited by the most scarce resource, which is mathematically expressed as the minimum of the integer divisions stock[i] / consumption[i] across all resource types. Naïve approaches that iterate over every possible inhabitant count or simulate consumption step‑by‑step quickly become infeasible for large inputs, as they can run in O(maxInhabitants * n) time. The optimal paradigm reduces the problem to a single linear scan: compute the division for each resource once and keep track of the minimum. This yields O(n) time and O(1) auxiliary space, making it scalable for millions of resource types.

Interview Questions on This Problem

Q1How would you determine the maximum number of inhabitants that can be sustained given arrays of stock and consumption values?

By performing a single pass over the arrays, computing stock[i] / consumption[i] for each resource, and returning the minimum of these quotients. This ensures we respect the most limiting resource.

Q2What is the time and space complexity of your solution, and why is it optimal?

The solution runs in O(n) time, where n is the number of resource types, because we only traverse each array once. It uses O(1) additional space, as we only maintain a running minimum. This is optimal because any algorithm must inspect each resource at least once to guarantee correctness.

Q3Can you explain a scenario where a greedy approach would fail if you tried to allocate resources incrementally?

If you allocate resources greedily by subtracting consumption from stock for each inhabitant until one resource is exhausted, you might prematurely stop when a less critical resource is depleted, missing the fact that another resource could still support more inhabitants. The correct approach is to compute the limiting resource upfront, not to simulate consumption step‑by‑step.

Examples

Example 1

Input

stock = [10, 20, 30], consumption = [2, 5, 10]

Output

5

Explanation: Calculate the maximum inhabitants supported by each resource individually: 1. Oxygen: 10 available / 2 required = 5 inhabitants. 2. Water: 20 available / 5 required = 4 inhabitants. 3. Food: 30 available / 10 required = 3 inhabitants. The limiting factor is Food, which supports only 3 inhabitants. Wait, let's re-calculate. 30/10 is 3. 20/5 is 4. 10/2 is 5. The minimum is 3. Let me correct the example values to ensure the output is 5 as stated in the draft or adjust the output. Let's use stock=[10, 25, 30], consumption=[2, 5, 10]. 1. 10/2 = 5. 2. 25/5 = 5. 3. 30/10 = 3. Min is 3. Let's try stock=[10, 20, 50], consumption=[2, 5, 10]. 1. 10/2 = 5. 2. 20/5 = 4. 3. 50/10 = 5. Min is 4. Let's try stock=[10, 25, 50], consumption=[2, 5, 10]. 1. 10/2 = 5. 2. 25/5 = 5. 3. 50/10 = 5. Min is 5. This works. Corrected Walkthrough: 1. Resource 1: 10 available / 2 required = 5. 2. Resource 2: 25 available / 5 required = 5. 3. Resource 3: 50 available / 10 required = 5. The minimum of these values is 5. Thus, 5 inhabitants can be supported.

Example 2

Input

stock = [100, 100, 100], consumption = [1, 2, 3]

Output

33

Explanation: Calculate the capacity for each resource: 1. Resource 1: 100 / 1 = 100. 2. Resource 2: 100 / 2 = 50. 3. Resource 3: 100 / 3 = 33 (integer division). The bottleneck is Resource 3, which limits the colony to 33 inhabitants.

Example 3

Input

stock = [5, 0, 10], consumption = [1, 1, 1]

Output

0

Explanation: Calculate the capacity for each resource: 1. Resource 1: 5 / 1 = 5. 2. Resource 2: 0 / 1 = 0. 3. Resource 3: 10 / 1 = 10. The minimum value is 0. Since the stock of Resource 2 is zero, no inhabitants can be supported.

Constraints

  • 1 <= stock.length <= 10^5
  • stock.length == consumption.length
  • 0 <= stock[i] <= 10^9
  • 1 <= consumption[i] <= 10^9

Optimal Approach & Strategy

The optimal approach performs a single pass over the arrays, computing the integer division for each resource and tracking the minimum. This yields O(n) time and O(1) space.

Brute Force Approach

A naive solution would try every possible inhabitant count from 0 up to the maximum stock, simulating consumption for each resource and stopping when any resource would go negative. This is O(maxInhabitants * n).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxInhabitants(available, required) {
    let minInhabitants = Infinity;
    for (let i = 0; i < available.length; i++) {
        const count = Math.floor(available[i] / required[i]);
        if (count < minInhabitants) {
            minInhabitants = count;
        }
    }
    return minInhabitants;
}

Asked in Top Tech Interviews

uncategorizedmediumgeneric

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.