BackmediumBinary Searchuncategorizedmedium

Mars Colony Resource Optimization Solution

Problem Statement

In the Mars colony resource management system, the colony's life support module requires a specific allocation of oxygen, water, and food to sustain the colonists. The system has a certain number of oxygen tanks, water containers, and food packets available. The colony's administrator needs to allocate these resources among a certain number of colonists, with each colonist requiring a specific amount of oxygen, water, and food. Calculate the maximum number of colonists that can be sustained with the given resources.

Example 1
Input
[10, 20, 30], [2, 3, 4], 5
Output
2

Explanation: Step-by-step: with input oxygen tanks = 10, water containers = 20, food packets = 30, colonist requirements = [2, 3, 4], and number of colonists = 5, we calculate the maximum number of colonists that can be sustained for each resource, then find the minimum among these values.

Example 2
Input
[50, 100, 150], [5, 10, 15], 10
Output
5

Explanation: Step-by-step: with input oxygen tanks = 50, water containers = 100, food packets = 150, colonist requirements = [5, 10, 15], and number of colonists = 10, we calculate the maximum number of colonists that can be sustained for each resource, then find the minimum among these values.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[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 Optimization — Problem Statement & Solution Guide

Binary SearchMediumMixed
TimeO(N log N)
|
SpaceO(N)

Problem Description

In the Mars colony resource management system, the colony's life support module requires a specific allocation of oxygen, water, and food to sustain the colonists. The system has a certain number of oxygen tanks, water containers, and food packets available. The colony's administrator needs to allocate these resources among a certain number of colonists, with each colonist requiring a specific amount of oxygen, water, and food. Calculate the maximum number of colonists that can be sustained with the given resources.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Mars Colony Resource Optimization"

medium

WHY DOES IT MATTER?

Sorting by a cost metric and binary searching on the cardinality transforms an exponential subset search into a polynomial‑time algorithm, making the problem tractable for large inputs. It also provides a clear, interview‑friendly strategy that demonstrates algorithmic thinking.

OPTIMIZATION CHALLENGE

The key insight is that the feasibility of a set depends only on the sum of its requirements, so the cheapest k colonists (by total requirement) are always the best candidate. This reduces the search space from 2^N subsets to a single sorted list and a binary search.

REAL-WORLD CONNECTION

In cloud resource scheduling, tasks are often sorted by CPU+memory usage and the scheduler decides how many can run concurrently. The same principle of ordering by total cost and binary searching on capacity applies.

Always precompute prefix sums after sorting; this turns each feasibility check into O(1) time and keeps the algorithm clean and fast. Also, be mindful of integer overflow when summing large resource amounts.

COMPLEXITY AT A GLANCE

⏱ Time:O(N log N)
đź’ľ Space:O(N)

Core Theory — Why This Approach?

The problem reduces to a classic resource‑allocation optimization where each colonist consumes a fixed amount of three independent resources (oxygen, water, food). A naive approach would enumerate all subsets of colonists to find the largest feasible set, which is exponential in the number of colonists and infeasible for large inputs. The key insight is that the feasibility of supporting a set of colonists depends only on the total consumption of each resource, not on the specific identities of the colonists. Therefore, if we sort colonists by the sum of their resource requirements (or by any monotonic combination that preserves feasibility), the k colonists with the smallest total consumption will always be the most “cheapest” to support. This allows us to use binary search on the answer k: for a candidate k we simply check whether the sum of the k smallest total requirements fits within the available resources. Sorting once gives O(N log N) time, and each feasibility check is O(k) or O(1) with prefix sums, yielding an overall O(N log N) solution. This paradigm—sorting by a cost metric and binary searching on the cardinality—avoids the combinatorial explosion of the brute‑force method and is optimal for this class of problems.

Interview Questions on This Problem

Q1How would you determine the maximum number of colonists that can be supported given limited oxygen, water, and food supplies?

I would model each colonist’s requirement as a triple of resource amounts. By sorting colonists by the sum of their requirements and using binary search on the number of colonists, I can check feasibility in O(1) with prefix sums. This gives an O(N log N) algorithm.

Q2Explain why a greedy approach that always picks the colonist with the smallest total resource requirement is optimal in this scenario.

Because resource consumption is additive and independent, any feasible set of k colonists can be replaced by the k with the smallest total consumption without violating any resource constraint. Thus the greedy choice preserves feasibility and maximizes the cardinality.

Q3What would happen if the resource requirements were not independent (e.g., oxygen consumption depends on water consumption)? How would you adapt the solution?

If dependencies exist, the problem becomes a multi‑dimensional knapsack and the greedy strategy may fail. I would then consider dynamic programming or integer linear programming, or use approximation algorithms like a greedy heuristic based on weighted sums or a branch‑and‑bound search.

Examples

Example 1

Input

[10, 20, 30], [2, 3, 4], 5

Output

2

Explanation: Step-by-step: with input oxygen tanks = 10, water containers = 20, food packets = 30, colonist requirements = [2, 3, 4], and number of colonists = 5, we calculate the maximum number of colonists that can be sustained for each resource, then find the minimum among these values.

Example 2

Input

[50, 100, 150], [5, 10, 15], 10

Output

5

Explanation: Step-by-step: with input oxygen tanks = 50, water containers = 100, food packets = 150, colonist requirements = [5, 10, 15], and number of colonists = 10, we calculate the maximum number of colonists that can be sustained for each resource, then find the minimum among these values.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Optimal Approach & Strategy

Sort colonists by the sum of their resource requirements. Compute prefix sums of each resource. Use binary search on the number of colonists k; for each k, check if the prefix sums for the first k colonists are within the available resources. This runs in O(N log N) time.

Brute Force Approach

Enumerate all subsets of colonists, compute the total resource consumption for each subset, and keep the largest subset that fits within the available resources. This is exponential time.

Verified Code Solutions

JavaScript Solution
Time: O(N log N)
function solution(oxygenTanks, waterContainers, foodPackets, colonistRequirements) {
       let maxColonistsOxygen = Math.floor(oxygenTanks / colonistRequirements[0]);
       let maxColonistsWater = Math.floor(waterContainers / colonistRequirements[1]);
       let maxColonistsFood = Math.floor(foodPackets / colonistRequirements[2]);
       return Math.min(maxColonistsOxygen, maxColonistsWater, maxColonistsFood);
   }

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.