BackmediumRecursionAtlassian

Interstellar Cargo Distribution Solution

Problem Statement

Given an array of positive integers representing the weights of cargo packages, determine how many distinct ways the packages can be split between two spacecraft, Starblade and NovaSpur, so that the total weight carried by each spacecraft is exactly the same. A split is defined by a subset of packages assigned to Starblade; the remaining packages automatically belong to NovaSpur. Two splits are considered identical if one can be obtained from the other by swapping the spacecraft, i.e., the unordered partition of the set matters. Return the count of such unordered partitions. The solution must be derived using a recursive approach (with memoization or pruning as needed).

Example 1
Input
[1,2,3,4,6]
Output
2

Explanation: Total weight = 1+2+3+4+6 = 16, half = 8. Subsets that sum to 8 are {2,6} and {1,3,4}. Each subset defines a unique unordered partition, giving 2 ways.

Example 2
Input
[5,5,5,5]
Output
6

Explanation: Total weight = 20, half = 10. Any choice of two 5‑weight packages forms a subset summing to 10. There are C(4,2)=6 such choices, and each leaves the remaining two 5‑weight packages for the other spacecraft. Hence 6 distinct partitions.

Example 3
Input
[1,1,1,1,1]
Output
0

Explanation: Total weight = 5, which is odd, so it is impossible to divide the cargo into two equal‑weight groups. Therefore the answer is 0.

Constraints

  • 1 <= nums.length <= 30
  • 1 <= nums[i] <= 10^4
  • All nums[i] are integers
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

Interstellar Cargo Distribution — Problem Statement & Solution Guide

RecursionMediumMixed
TimeO(n*target)
|
SpaceO(n*target)

Problem Description

Given an array of positive integers representing the weights of cargo packages, determine how many distinct ways the packages can be split between two spacecraft, Starblade and NovaSpur, so that the total weight carried by each spacecraft is exactly the same. A split is defined by a subset of packages assigned to Starblade; the remaining packages automatically belong to NovaSpur. Two splits are considered identical if one can be obtained from the other by swapping the spacecraft, i.e., the unordered partition of the set matters. Return the count of such unordered partitions. The solution must be derived using a recursive approach (with memoization or pruning as needed).

DSA Pattern Breakdown

DSA Pattern Breakdown

"Interstellar Cargo Distribution"

medium

WHY DOES IT MATTER?

Subset‑sum counting is a fundamental DP pattern that appears in resource allocation, load balancing, and cryptographic knapsack problems; mastering it equips engineers to tackle many NP‑hard variants efficiently on bounded inputs.

OPTIMIZATION CHALLENGE

The key insight is to recognize that the total sum must be even and then to count subsets that hit sum/2, allowing us to prune half the search space and replace exponential recursion with memoized states (i,remaining).

REAL-WORLD CONNECTION

Think of distributing cargo across two parallel data‑center clusters where each cluster must handle equal load; the DP mirrors how a scheduler decides which jobs (packages) go to which cluster to achieve perfect balance.

During an interview, first compute total weight, early‑exit if odd, then implement a recursive function with a cache (e.g., unordered_map or vector) – this shows you understand both the mathematical reduction and practical memoization.

COMPLEXITY AT A GLANCE

⏱ Time:O(n*target)
💾 Space:O(n*target)

Core Theory — Why This Approach?

The problem reduces to counting subsets whose sum equals half of the total weight. A naive recursive enumeration tries every inclusion/exclusion choice, leading to O(2^n) time which explodes even for moderate n. By recognizing the sub‑problem – “how many ways can we achieve a target sum using the first i items?” – we can apply recursion with memoization (top‑down DP) or iterative DP (bottom‑up) to reuse overlapping sub‑problems. This transforms the exponential search space into a pseudo‑polynomial one, O(n*target), where target = total/2, because each state (i,remaining) is solved at most once. The optimal paradigm is thus a classic subset‑sum counting DP, leveraging the principle of optimality and overlapping sub‑problems inherent to recursion with memoization.

Interview Questions on This Problem

Q1How would you modify the solution if the cargo weights could be negative?

Negative numbers break the simple DP table indexed by sum because the range can become unbounded. You would shift the possible sum range by adding an offset equal to the absolute sum of negative numbers, or use a hashmap‑based memoization that maps (index, currentSum) to count, thereby handling arbitrary integer sums.

Q2What is the time‑space trade‑off when using a 1‑dimensional DP array versus a 2‑dimensional DP table for this problem?

A 2‑D table dp[i][s] stores counts for each prefix i and sum s, using O(n*target) space. By iterating items and updating a 1‑D array from high to low sums, we collapse the dimension to O(target) space, but we lose the ability to reconstruct the exact subsets without extra bookkeeping.

Q3Can you extend the algorithm to return the actual subsets, not just the count, while keeping the same asymptotic complexity?

Returning all subsets inherently requires O(k) extra space where k is the number of valid subsets, which can be exponential. However, you can generate subsets on‑the‑fly using backtracking guided by the DP table, still O(n*target) time for enumeration, but the overall space becomes O(n+target) plus the output size.

Examples

Example 1

Input

[1,2,3,4,6]

Output

2

Explanation: Total weight = 1+2+3+4+6 = 16, half = 8. Subsets that sum to 8 are {2,6} and {1,3,4}. Each subset defines a unique unordered partition, giving 2 ways.

Example 2

Input

[5,5,5,5]

Output

6

Explanation: Total weight = 20, half = 10. Any choice of two 5‑weight packages forms a subset summing to 10. There are C(4,2)=6 such choices, and each leaves the remaining two 5‑weight packages for the other spacecraft. Hence 6 distinct partitions.

Example 3

Input

[1,1,1,1,1]

Output

0

Explanation: Total weight = 5, which is odd, so it is impossible to divide the cargo into two equal‑weight groups. Therefore the answer is 0.

Constraints

  • 1 <= nums.length <= 30
  • 1 <= nums[i] <= 10^4
  • All nums[i] are integers

Optimal Approach & Strategy

Use DP (recursion + memoization or iterative table) to count ways to reach the target sum, reducing complexity to O(n*target).

Brute Force Approach

Enumerate every possible subset (2^n) and count those whose sum equals half of the total weight.

Verified Code Solutions

JavaScript Solution
Time: O(n*target)
function countWays(weights){
    const total=weights.reduce((a,b)=>a+b,0);
    if(total%2!==0) return 0;
    const target=total/2;
    const n=weights.length;
    const memo=Array.from({length:n},()=>Array(target+1).fill(undefined));
    function dfs(idx,rem){
        if(rem===0) return 1;
        if(idx===n||rem<0) return 0;
        if(memo[idx][rem]!==undefined) return memo[idx][rem];
        const take=dfs(idx+1,rem-weights[idx]);
        const skip=dfs(idx+1,rem);
        return memo[idx][rem]=take+skip;
    }
    return dfs(0,target);
}
function main(){
    const fs=require('fs');
    const data=fs.readFileSync(0,'utf8').trim().split(/\s+/).map(Number);
    if(data.length===0) return;
    const n=data[0];
    const weights=data.slice(1,1+n);
    console.log(countWays(weights));
}
main();

Asked in Top Tech Interviews

Atlassian

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.