Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Alice had a 0-indexed array arr consisting of n positive integers. She chose an arbitrary positive integer k and created two new 0-indexed integer arrays lower and higher in the following manner:
lower[i] = arr[i] - k, for every index i where 0 <= i < nhigher[i] = arr[i] + k, for every index i where 0 <= i < nUnfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays lower and higher, but not the array each integer belonged to. Help Alice and recover the original array.
Given an array nums consisting of 2n integers, where exactly n of the integers were present in lower and the remaining in higher, return the original array arr. In case the answer is not unique, return any valid array.
Note: The test cases are generated such that there exists at least one valid array arr.
Example 1:
Input: nums = [2,10,6,4,8,12] Output: [3,7,11] Explanation: If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12]. Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums. Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12].
Example 2:
Input: nums = [1,1,3,3] Output: [2,2] Explanation: If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3]. Combining lower and higher gives us [1,1,3,3], which is equal to nums. Note that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0. This is invalid since k must be positive.
Example 3:
Input: nums = [5,435] Output: [220] Explanation: The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].
Constraints:
2 * n == nums.length1 <= n <= 10001 <= nums[i] <= 109arr.Problem summary: Alice had a 0-indexed array arr consisting of n positive integers. She chose an arbitrary positive integer k and created two new 0-indexed integer arrays lower and higher in the following manner: lower[i] = arr[i] - k, for every index i where 0 <= i < n higher[i] = arr[i] + k, for every index i where 0 <= i < n Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays lower and higher, but not the array each integer belonged to. Help Alice and recover the original array. Given an array nums consisting of 2n integers, where exactly n of the integers were present in lower and the remaining in higher, return the original array arr. In case the answer is not unique, return any valid array. Note: The test cases are generated such that there exists at least one valid array arr.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers
[2,10,6,4,8,12]
[1,1,3,3]
[5,435]
find-array-given-subset-sums)find-original-array-from-doubled-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2122: Recover the Original Array
class Solution {
public int[] recoverArray(int[] nums) {
Arrays.sort(nums);
for (int i = 1, n = nums.length; i < n; ++i) {
int d = nums[i] - nums[0];
if (d == 0 || d % 2 == 1) {
continue;
}
boolean[] vis = new boolean[n];
vis[i] = true;
List<Integer> t = new ArrayList<>();
t.add((nums[0] + nums[i]) >> 1);
for (int l = 1, r = i + 1; r < n; ++l, ++r) {
while (l < n && vis[l]) {
++l;
}
while (r < n && nums[r] - nums[l] < d) {
++r;
}
if (r == n || nums[r] - nums[l] > d) {
break;
}
vis[r] = true;
t.add((nums[l] + nums[r]) >> 1);
}
if (t.size() == (n >> 1)) {
int[] ans = new int[t.size()];
int idx = 0;
for (int e : t) {
ans[idx++] = e;
}
return ans;
}
}
return null;
}
}
// Accepted solution for LeetCode #2122: Recover the Original Array
func recoverArray(nums []int) []int {
sort.Ints(nums)
for i, n := 1, len(nums); i < n; i++ {
d := nums[i] - nums[0]
if d == 0 || d%2 == 1 {
continue
}
vis := make([]bool, n)
vis[i] = true
ans := []int{(nums[0] + nums[i]) >> 1}
for l, r := 1, i+1; r < n; l, r = l+1, r+1 {
for l < n && vis[l] {
l++
}
for r < n && nums[r]-nums[l] < d {
r++
}
if r == n || nums[r]-nums[l] > d {
break
}
vis[r] = true
ans = append(ans, (nums[l]+nums[r])>>1)
}
if len(ans) == (n >> 1) {
return ans
}
}
return []int{}
}
# Accepted solution for LeetCode #2122: Recover the Original Array
class Solution:
def recoverArray(self, nums: List[int]) -> List[int]:
nums.sort()
n = len(nums)
for i in range(1, n):
d = nums[i] - nums[0]
if d == 0 or d % 2 == 1:
continue
vis = [False] * n
vis[i] = True
ans = [(nums[0] + nums[i]) >> 1]
l, r = 1, i + 1
while r < n:
while l < n and vis[l]:
l += 1
while r < n and nums[r] - nums[l] < d:
r += 1
if r == n or nums[r] - nums[l] > d:
break
vis[r] = True
ans.append((nums[l] + nums[r]) >> 1)
l, r = l + 1, r + 1
if len(ans) == (n >> 1):
return ans
return []
// Accepted solution for LeetCode #2122: Recover the Original Array
/**
* [2122] Recover the Original Array
*
* Alice had a 0-indexed array arr consisting of n positive integers. She chose an arbitrary positive integer k and created two new 0-indexed integer arrays lower and higher in the following manner:
* <ol>
* lower[i] = arr[i] - k, for every index i where 0 <= i < n
* higher[i] = arr[i] + k, for every index i where 0 <= i < n
* </ol>
* Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays lower and higher, but not the array each integer belonged to. Help Alice and recover the original array.
* Given an array nums consisting of 2n integers, where exactly n of the integers were present in lower and the remaining in higher, return the original array arr. In case the answer is not unique, return any valid array.
* Note: The test cases are generated such that there exists at least one valid array arr.
*
* Example 1:
*
* Input: nums = [2,10,6,4,8,12]
* Output: [3,7,11]
* Explanation:
* If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12].
* Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums.
* Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12].
*
* Example 2:
*
* Input: nums = [1,1,3,3]
* Output: [2,2]
* Explanation:
* If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3].
* Combining lower and higher gives us [1,1,3,3], which is equal to nums.
* Note that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0.
* This is invalid since k must be positive.
*
* Example 3:
*
* Input: nums = [5,435]
* Output: [220]
* Explanation:
* The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].
*
*
* Constraints:
*
* 2 * n == nums.length
* 1 <= n <= 1000
* 1 <= nums[i] <= 10^9
* The test cases are generated such that there exists at least one valid array arr.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/recover-the-original-array/
// discuss: https://leetcode.com/problems/recover-the-original-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn recover_array(nums: Vec<i32>) -> Vec<i32> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2122_example_1() {
let nums = vec![2, 10, 6, 4, 8, 12];
let result = vec![3, 7, 11];
assert_eq!(Solution::recover_array(nums), result);
}
#[test]
#[ignore]
fn test_2122_example_2() {
let nums = vec![1, 1, 3, 3];
let result = vec![2, 2];
assert_eq!(Solution::recover_array(nums), result);
}
#[test]
#[ignore]
fn test_2122_example_3() {
let nums = vec![5, 435];
let result = vec![220];
assert_eq!(Solution::recover_array(nums), result);
}
}
// Accepted solution for LeetCode #2122: Recover the Original Array
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2122: Recover the Original Array
// class Solution {
// public int[] recoverArray(int[] nums) {
// Arrays.sort(nums);
// for (int i = 1, n = nums.length; i < n; ++i) {
// int d = nums[i] - nums[0];
// if (d == 0 || d % 2 == 1) {
// continue;
// }
// boolean[] vis = new boolean[n];
// vis[i] = true;
// List<Integer> t = new ArrayList<>();
// t.add((nums[0] + nums[i]) >> 1);
// for (int l = 1, r = i + 1; r < n; ++l, ++r) {
// while (l < n && vis[l]) {
// ++l;
// }
// while (r < n && nums[r] - nums[l] < d) {
// ++r;
// }
// if (r == n || nums[r] - nums[l] > d) {
// break;
// }
// vis[r] = true;
// t.add((nums[l] + nums[r]) >> 1);
// }
// if (t.size() == (n >> 1)) {
// int[] ans = new int[t.size()];
// int idx = 0;
// for (int e : t) {
// ans[idx++] = e;
// }
// return ans;
// }
// }
// return null;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.