We have two arrays of positive integers. For every possible pair where one number comes from arr1 and the other from arr2, we need to find how many leading digits they share, and return the maximum such count across all pairs.
For example, if arr1 has the number 12345 and arr2 has 12367, their common prefix is "123", so they share 3 leading digits. We want the pair across the two arrays that shares the longest such leading sequence.
We don't need to check every pair individually. The prefixes of a number form a small set (at most 9 of them). If we store every prefix that appears in one array, we can look up whether the other array's numbers reuse any of those prefixes. This turns "compare all pairs" into "which prefixes do the two arrays have in common?"
1 <= arr1.length, arr2.length <= 5 * 10^4 → With up to 50,000 elements in each array, a brute force approach that checks all pairs would do up to 2.5 billion pair comparisons. That's too slow. We need something better than O(m * n).1 <= arr1[i], arr2[i] <= 10^8 → Each number has at most 9 digits. This means the "prefix work" per number is bounded by a small constant (at most 9 prefixes per number). Any approach that does O(d) work per number, where d is the number of digits, is effectively linear in the array size.For every pair (x, y) where x comes from arr1 and y comes from arr2, convert both numbers to strings and compare them character by character from the left. Count how many leading characters match, and track the maximum across all pairs.
This mirrors comparing two numbers by hand: line them up at the leftmost digit and count how many positions agree before the first mismatch.
maxLength = 0.x in arr1:y in arr2:maxLength with the count if it's larger.maxLength.Loading animation...
Comparing every pair is too slow for the given constraints. The next approach precomputes all the prefixes from one array once, then reuses them for every lookup.
Instead of comparing pairs of numbers, we can split the problem into two phases. First, collect every prefix from all numbers in arr1. Then, for each number in arr2, generate its prefixes and check which ones already appear in that collection.
To generate all prefixes of a number, repeatedly divide by 10. Take 12345: dividing gives 12345, 1234, 123, 12, 1, where each division strips the last digit to produce the next shorter prefix. A prefix represented as an integer is unique, so two numbers share leading digits exactly when they produce the same prefix value.
Store all prefixes from arr1 in a hash set. Then for each number in arr2, generate its prefixes from longest to shortest and check each against the set. The longest match found across all arr2 numbers is the answer.
In Phase 2 we start from the full arr2 number and divide by 10, so we test prefixes from longest to shortest. If a prefix of length k is in the set, then every shorter prefix of the same number is also in the set (the set was built by the same divide-by-10 process on arr1). The first match is therefore the longest possible for that number, and the break skips the redundant shorter checks.
prefixSet.x in arr1:x > 0, add x to prefixSet, then set x = x / 10.maxLength = 0.y in arr2:y > 0:y is in prefixSet, update maxLength with the number of digits in y, then break (longest prefix for this number found).y = y / 10.maxLength.Loading animation...
The hash set stores each prefix as a separate entry, even when many numbers share the same leading digits. The next approach uses a trie, which stores shared prefixes once as a common path.
A trie (prefix tree) stores prefixes as paths in a tree. Each edge represents one digit, and each path from the root to a node spells out a prefix. Building the trie from arr1 makes every prefix of every arr1 number a path, with shared leading digits collapsed onto a single shared path.
Take the number 12345. We insert the digits 1, 2, 3, 4, 5 as a path. To compare 12367 against it, we walk the trie following 1, 2, 3, then stop at digit 6 because that node has no child for 6 (it has a child for 4). The depth reached, 3, is the longest common prefix between 12367 and 12345.
Sharing is what makes this compact. If arr1 has 123, 1234, and 456, the trie stores the path 1 to 2 to 3 to 4 once rather than once per number, plus a separate branch 4 to 5 to 6.
arr1:arr2, traverse the trie:Loading animation...