Maximum Length of Identical Merged Arrays
Arrays & HashingAuto-graded · 23 test casesLast updated
Find the maximum size of two matching arrays derived by repeatedly combining adjacent elements from two positive integer sequences, or return -1 if impossible.
You are given two sequences of positive integers, a and b. You can modify an array by selecting any two neighboring elements and replacing them with their sum. This merge step can be applied any number of times to either or both arrays.
Determine the maximum possible length of the resulting arrays such that array a and array b become identical (meaning both arrays have equal length and matching values at every corresponding position).
If it is impossible to transform a and b into identical sequences, return -1.
Constraints
- $1 \le \text{len}(a), \text{len}(b) \le 200{,}000$
- $1 \le a[i], b[i] \le 10^9$
- The total sum of elements in each list fits within a standard 64-bit signed integer.
Examples
Example 1
Input: max_equal_array_length([3, 2, 6, 2], [5, 1, 5, 2])
Output: 3
Example 2
Input: max_equal_array_length([1, 2, 3], [4, 2])
Output: 1
Example 3
Input: max_equal_array_length([1, 2, 4], [4, 2])
Output: -1
Hints
Hint 1
Consider what property is preserved when adjacent elements in an array are merged into a single value.
Hint 2
Because all elements are strictly positive, cumulative running sums (prefix sums) strictly increase.
Hint 3
What is the relationship between shared prefix sums of the two arrays and valid common segment boundaries?
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
When adjacent elements are combined, the total sum of the array remains unchanged. Consequently, two arrays can only become identical if their total sums are equal. If sum(a) != sum(b), it is impossible to make them equal, so we immediately return -1.
Each merge operation retains a subset of contiguous cuts in the original sequence. Because every number in $a$ and $b$ is strictly positive, the prefix sum up to any cut is strictly increasing. Therefore, a boundary location can be uniquely identified by its prefix sum value:
- Any element in the final merged array corresponds to the sum of a contiguous subsegment.
- A split between two segments in the merged array occurs at a specific prefix sum value.
- A split is valid for both arrays simultaneously if and only if both arrays share that identical prefix sum at some internal index.
To maximize the length of the resulting array, we should keep every available shared interior prefix sum as a split boundary. If there are $k$ matching interior prefix sums, the merged arrays will consist of $k + 1$ segments.
Implementation
- Compare
sum(a)andsum(b). If they differ, return-1. - Compute all proper prefix sums of array
a(excluding the last element) and store them in a hash set. - Iterate through array
b(excluding the last element), calculating its running prefix sum and checking how many exist ina's hash set. - Return the count of common interior prefix sums plus 1.
Alternatively, a two-pointer approach tracking running sums in $O(1)$ auxiliary space can be used with the same overall complexity.
Complexity
- Time Complexity: $\mathcal{O}(n + m)$ where $n$ and $m$ are the lengths of
aandb. Computing total sums and building/querying the hash set takes linear time. - Space Complexity: $\mathcal{O}(n)$ auxiliary space to store the prefix sums of
ain a hash set (or $\mathcal{O}(1)$ extra space if using two pointers).
from typing import List
def max_equal_array_length(a: List[int], b: List[int]) -> int:
if sum(a) != sum(b):
return -1
prefix = 0
boundaries = set()
for value in a[:-1]:
prefix += value
boundaries.add(prefix)
prefix = 0
common = 0
for value in b[:-1]:
prefix += value
if prefix in boundaries:
common += 1
return common + 1