BackmediumArraysPhonePe

Partition Array by Weight Solution

Problem Statement

Given an array of integers weights representing the weights of crates, determine if it is possible to divide the array into two parts with equal total weight. The division can occur at any index in the array, including the start or end.

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

Explanation: Step-by-step: The total weight of the array is 16. If we divide the array at index 4, we get two parts with total weights 10 and 6, which are not equal. Therefore, the function should return false.

Example 2
Input
[1, 1, 1, 1, 1]
Output
true

Explanation: Step-by-step: The total weight of the array is 5. If we divide the array at index 2, we get two parts with total weights 3 and 2, which are not equal. However, if we divide the array at index 4, we get two parts with total weights 3 and 2, which are equal. Therefore, the function should return true.

Constraints

  • 1 <= number of crates <= 10^5
  • 1 <= weight of each crate <= 10^5
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

Partition Array by Weight — Problem Statement & Solution Guide

ArraysMediumTwo Pointers
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of integers weights representing the weights of crates, determine if it is possible to divide the array into two parts with equal total weight. The division can occur at any index in the array, including the start or end.

Examples

Example 1

Input

[1, 2, 3, 4, 6]

Output

false

Explanation: Step-by-step: The total weight of the array is 16. If we divide the array at index 4, we get two parts with total weights 10 and 6, which are not equal. Therefore, the function should return false.

Example 2

Input

[1, 1, 1, 1, 1]

Output

true

Explanation: Step-by-step: The total weight of the array is 5. If we divide the array at index 2, we get two parts with total weights 3 and 2, which are not equal. However, if we divide the array at index 4, we get two parts with total weights 3 and 2, which are equal. Therefore, the function should return true.

Constraints

  • 1 <= number of crates <= 10^5
  • 1 <= weight of each crate <= 10^5

Optimal Approach & Strategy

The optimal approach involves calculating the total weight of all crates in a single pass and then using a Two Pointers technique to find the division point, resulting in a time complexity of O(n).

Brute Force Approach

A naive approach would involve checking all possible division points, resulting in a time complexity of O(n²). This can be done by iterating over the list of crates and calculating the total weight on both sides of each possible division point.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function partitionArrayByWeight(weights) {
  if (weights.length < 2) return false;
  let sum = 0;
  for (let weight of weights) {
    sum += weight;
  }
  return sum % 2 === 0;
}

Asked in Top Tech Interviews

PhonePe

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.