cswork / libraryOpen practice workspace

Coding questions · Microsoft

Maximize Equal Array Length After Contiguous Sum Merges

Find the maximum length of equal arrays obtainable by merging adjacent positive values into their sums, or report that equality is impossible.

You are given two nonempty arrays of positive integers, a and b. In either array, you may replace a contiguous group of elements with one element equal to their sum. You may repeat this operation independently on both arrays.

Implement maxCommonLength(a, b). Return the maximum possible length of the resulting arrays when they are exactly equal, element by element. Return -1 if they cannot be made equal.

Rules and Constraints

  • Merging preserves element order. You may merge adjacent groups but cannot split an element, reorder elements, or remove values.
  • You may perform no operations on an array that already has the desired form.
  • Each array has between 1 and 100,000 elements. Every value is between 1 and 1,000,000,000.
  • Intermediate and total sums must be represented exactly.

Example 1

 a = [1, 2, 3, 3]
 b = [3, 3, 3]
Output: 3

Merge the first two elements of a to obtain [3, 3, 3]. Length 3 is maximal because b starts with only three elements and merging cannot increase its length.

Example 2

 a = [1, 2]
 b = [4]
Output: -1

No allowed sequence of merges makes these arrays equal.

Maximize Equal Array Length After Contiguous Sum Merges

Approach

A resulting element corresponds to a contiguous segment of each original array. Therefore each boundary between completed segments must be a cumulative sum that appears in both arrays. Since every input element is positive, each array's prefix sums are strictly increasing.

Use two indices and two cumulative sums. Advance the side with the smaller cumulative sum; when they are equal, advance either available side. Count each equality reached after an advance. Once one side is exhausted, advance the remaining side until both are exhausted. If the final totals differ, return -1; otherwise return the number of common positive prefix sums found.

This is a merge of two increasing prefix-sum sequences. When one current sum is smaller, it cannot equal the other current sum or any later, larger sum until its own index advances. Thus the scan misses no common boundary and counts each once. All common boundaries can be used simultaneously in increasing order: consecutive common sums define equal positive segment totals on the two sides. This constructs a valid result with one element per common boundary, while no result can have more boundaries than the number shared. Equal totals always include the final boundary; unequal totals cannot be repaired because merging preserves the total.

Each iteration consumes exactly one input element, giving O(a.length + b.length) time and O(1) auxiliary space. Java and C++ accumulate in 64-bit integers. Python integers are exact, and every allowed JavaScript sum is at most 10^14, below its exact-integer limit 2^53.

  • Time complexity: O(a.length + b.length).
  • Space complexity: O(1) auxiliary space.