Problem

About Shipping

Learn this problem
JPMorgan ChaseINTERNOA

Problem statement

A shop has n item types. The quantity of the i-th item type is quantity[i], using 1-based indexing in the description.

The items must be shipped in two consecutive consignments split by an index j: the first consignment contains item types [1, 2, ..., j], and the second contains item types [j + 1, ..., n]. Choose j such that 1 <= j < n, so both consignments are non-empty.

In one operation, increase or decrease any quantity[i] by 1. Quantities must remain positive. Return the minimum number of operations needed to make the total quantities in the two consignments equal, choosing the split optimally.

Function

getMinimumOperations(quantity: int[]) → long

Complete the function getMinimumOperations in the editor with the following parameter:

  • int quantity[n]: the quantities of each item type

Returns

  • long: the minimum number of operations required to make the sums equal in an optimal division

Examples

Example 1

quantity = [1, 4, 4]return = 1

Increase quantity[3] by 1 to get [1, 4, 5]. Splitting at j = 2 gives consignments with sums 5 and 5, so the answer is 1.

Example 2

quantity = [2, 2]return = 0

The only split already has equal sums, so no operation is needed.

Constraints

  • 2 <= quantity.length <= 200000
  • 1 <= quantity[i] <= 1000000000
  • The answer can be large, so use 64-bit arithmetic in fixed-width languages.

More JPMorgan Chase problems

drafts saved locally
public long getMinimumOperations(int[] quantity) {
  // write your code here
}
quantity[1, 4, 4]
expected1
checking account