BackmediumBacktrackingSwiggy

Cargo Bay Organization Solution

Problem Statement

You are given an array of integers representing the weights of cargo crates and an integer k that denotes the number of cargo bays. Your task is to distribute every crate into exactly k non‑empty bays. The total weight of a bay is the sum of the weights of the crates assigned to it. The quality of a distribution is measured by the difference between the heaviest and the lightest bay totals. Compute the smallest possible value of this difference.

Input format:

  • The first line contains two integers n and k (1 ≤ k ≤ n ≤ 10^5), the number of crates and the number of bays.
  • The second line contains n integers w1, w2, …, wn (−10^9 ≤ wi ≤ 10^9), the weights of the crates.

Output format:

  • Output a single integer: the minimal possible difference between the maximum and minimum bay totals.

The crates can be assigned to bays in any order; the only requirement is that each bay receives at least one crate.

Example 1
Input
5 2 1 2 3 4 5
Output
5

Explanation: We must split the five crates into two non‑empty groups. The best we can do is to make one group sum to 10 and the other to 5. For example, group A = {1,2,3,4} (sum = 10) and group B = {5} (sum = 5). The difference is 10 − 5 = 5. No other partition yields a smaller difference, so the answer is 5.

Example 2
Input
4 3 10 20 30 40
Output
10

Explanation: With four crates and three bays, each bay must contain at least one crate. One optimal assignment is: bay 1 = {10,20} (sum = 30), bay 2 = {30} (sum = 30), bay 3 = {40} (sum = 40). The maximum total is 40, the minimum is 30, giving a difference of 10. Any other arrangement results in a larger difference, so 10 is minimal.

Example 3
Input
6 3 5 5 5 5 5 5
Output
0

Explanation: All crates weigh the same. Distribute two crates to each bay: each bay sum is 10. The maximum and minimum totals are both 10, so the difference is 0, which is the smallest possible value.

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ k ≤ n
  • −10^9 ≤ wi ≤ 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

Cargo Bay Organization — Problem Statement & Solution Guide

BacktrackingMediumMixed
TimeO(k^n)
|
SpaceO(n)

Problem Description

You are given an array of integers representing the weights of cargo crates and an integer k that denotes the number of cargo bays. Your task is to distribute every crate into exactly k non‑empty bays. The total weight of a bay is the sum of the weights of the crates assigned to it. The quality of a distribution is measured by the difference between the heaviest and the lightest bay totals. Compute the smallest possible value of this difference.

Input format:

- The first line contains two integers n and k (1 ≤ k ≤ n ≤ 10^5), the number of crates and the number of bays.

- The second line contains n integers w1, w2, …, wn (−10^9 ≤ wi ≤ 10^9), the weights of the crates.

Output format:

- Output a single integer: the minimal possible difference between the maximum and minimum bay totals.

The crates can be assigned to bays in any order; the only requirement is that each bay receives at least one crate.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Cargo Bay Organization"

medium

WHY DOES IT MATTER?

Backtracking is essential for solving combinatorial optimization problems where the search space is too large for brute force but too complex for greedy or dynamic programming approaches. It allows for systematic exploration of all possible solutions while pruning infeasible branches.

OPTIMIZATION CHALLENGE

The key insight is to sort the items in descending order and prune branches where the current bay sum exceeds the target maximum sum. Additionally, skipping symmetric states (e.g., two bays with the same sum) reduces the search space significantly.

REAL-WORLD CONNECTION

This pattern is analogous to task scheduling in distributed systems, where tasks (crates) are assigned to workers (bays) to minimize the makespan (difference between the longest and shortest worker loads).

In interviews, always mention the sorting step and the pruning conditions. This shows you understand how to reduce the search space and is a key differentiator from a naive backtracking solution.

COMPLEXITY AT A GLANCE

⏱ Time:O(k^n)
💾 Space:O(n)

Core Theory — Why This Approach?

The problem of distributing items into k bins to minimize the difference between the maximum and minimum bin sums is a variant of the Partition Equal Subset Sum problem, generalized to k partitions. It is an NP-hard problem, meaning there is no known polynomial-time algorithm that solves it exactly for all inputs. The search space grows exponentially with the number of items, as each item can be placed in any of the k bays, leading to a naive complexity of O(k^n). This makes brute-force enumeration infeasible for even moderate input sizes (e.g., n > 20).

Interview Questions on This Problem

Q1At a major logistics company, how would you adapt this algorithm if the cargo weights were not integers but floating-point numbers, and the bays had capacity constraints?

You would need to handle floating-point precision issues by scaling weights to integers if possible, or using epsilon comparisons. Capacity constraints add a pruning condition: if adding a crate exceeds the bay's capacity, that branch is pruned. The core backtracking logic remains, but the state space is further reduced by these constraints.

Q2In a fintech platform, how would you ensure that the distribution of transaction loads across k servers minimizes the maximum load, and what is the time complexity of your solution?

This is a classic load balancing problem. You can use backtracking with pruning to find the optimal distribution. The time complexity is O(k^n) in the worst case, but with effective pruning (e.g., sorting items in descending order and skipping symmetric states), it can be significantly reduced in practice.

Q3At a high-growth engineering startup, how would you optimize the backtracking algorithm to handle large inputs efficiently, and what data structures would you use?

You can use memoization to store the state of the bays (sorted to avoid symmetric states) and the index of the current item. This reduces the time complexity to O(n * k^n) in the worst case, but with memoization, it can be much faster. You would use a hash map to store the memoized states.

Examples

Example 1

Input

5 2
1 2 3 4 5

Output

5

Explanation: We must split the five crates into two non‑empty groups. The best we can do is to make one group sum to 10 and the other to 5. For example, group A = {1,2,3,4} (sum = 10) and group B = {5} (sum = 5). The difference is 10 − 5 = 5. No other partition yields a smaller difference, so the answer is 5.

Example 2

Input

4 3
10 20 30 40

Output

10

Explanation: With four crates and three bays, each bay must contain at least one crate. One optimal assignment is: bay 1 = {10,20} (sum = 30), bay 2 = {30} (sum = 30), bay 3 = {40} (sum = 40). The maximum total is 40, the minimum is 30, giving a difference of 10. Any other arrangement results in a larger difference, so 10 is minimal.

Example 3

Input

6 3
5 5 5 5 5 5

Output

0

Explanation: All crates weigh the same. Distribute two crates to each bay: each bay sum is 10. The maximum and minimum totals are both 10, so the difference is 0, which is the smallest possible value.

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ k ≤ n
  • −10^9 ≤ wi ≤ 10^9

Optimal Approach & Strategy

Use backtracking with pruning by sorting crates in descending order and skipping branches where the bay sum exceeds the current best maximum sum. Additionally, skip symmetric states where two bays have the same sum to reduce redundant calculations.

Brute Force Approach

Try all possible ways to distribute the crates into k bays and calculate the difference between the heaviest and lightest bay for each distribution. Keep track of the minimum difference found.

Verified Code Solutions

JavaScript Solution
Time: O(k^n)
function cargoBayOrganization(crates, bays) {
  if (crates.length === 0) return 0;
  if (bays === 1) return -1;
  let totalWeight = crates.reduce((a, b) => a + b, 0);
  let targetWeight = Math.floor(totalWeight / bays);
  let result = -1;
  function backtrack(index, currentBays, currentWeight) {
    if (index === crates.length) {
      if (currentBays === bays && currentWeight === targetWeight * bays) {
        result = 1;
      }
      return;
    }
    for (let i = 0; i <= currentBays; i++) {
      if (currentWeight + crates[index] <= targetWeight * (i + 1)) {
        backtrack(index + 1, i + 1, currentWeight + crates[index]);
      }
    }
  }
  backtrack(0, 1, 0);
  return result;
}

Asked in Top Tech Interviews

Swiggy

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.