BackmediumArrays Adobe

Cargo Ship Balancing Solution

Problem Statement

Given a cargo ship with a list of crates of different weights, determine if it's possible to balance the cargo by dividing it into two parts with equal total weight. The division can be done at any point in the list of crates.

Example 1
Input
[10, 20, 40]
Output
false

Explanation: Step-by-step: with input [10, 20, 40], we calculate the prefix sum array as [10, 30, 70]. We then check if the sum of the prefix sum array from index 0 to the middle index is equal to the sum of the prefix sum array from the middle index + 1 to the end. Since 10 + 30 is not equal to 70, we return false.

Example 2
Input
[1, 2, 3, 4, 5]
Output
false

Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we calculate the prefix sum array as [1, 3, 6, 10, 15]. We then check if the sum of the prefix sum array from index 0 to the middle index is equal to the sum of the prefix sum array from the middle index + 1 to the end. Since 1 + 3 + 6 is not equal to 10 + 15, we return false.

Constraints

  • 1 <= number of crates <= 10^5
  • 1 <= weight of each crate <= 10^5
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free