The Recover the Original Array problem gives you a sorted array that was created by combining [arr[i]-k, arr[i]+k] for each element of the original array. Reconstruct the original array. This hard coding problem tries all valid k values (derived from the first element's possible pairings) and checks feasibility with a frequency map. The array, hash table, sorting, two pointers, and enumeration interview pattern is the core.
Google asks this because it tests the "enumerate possible k values, validate each" strategy. The first element of the sorted combined array must pair with some other element (offset by 2k). Trying each pairing as a candidate k narrows the search space dramatically.
Enumerate k + frequency validation. Sort the combined array. The smallest element must pair with another element (at offset 2k from it). Enumerate all k = (combined[j] - combined[0]) / 2 for j=1,2,...n-1 (only if even difference and k>0). For each valid k: use a frequency map to greedily pair x with x+2k for each element from smallest to largest. If all pairs match perfectly, this k gives the original array.
changed=[2,10,6,4,8,12] (sorted: [2,4,6,8,10,12]). n=3.
Recover the Original Array uses "enumerate and validate" — a powerful technique when direct computation is hard but validation is easy. Enumerate candidate k values from the first element's pairings, then validate each O(n) with a frequency map. Practice similar enumeration problems: "find the duplicate," "reconstruct from modified array." The first-element constraint is key for narrowing candidates.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Max Number of K-Sum Pairs | Medium | Solve | |
| Divide Players Into Teams of Equal Skill | Medium | Solve | |
| Find the Integer Added to Array II | Medium | Solve | |
| Number of Arithmetic Triplets | Easy | Solve | |
| Largest Positive Integer That Exists With Its Negative | Easy | Solve |