LeetCode 2122: Recover the Original Array Solution
Master LeetCode problem 2122 (Recover the Original Array), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2122. Recover the Original Array
Problem Explanation
Explanation:
To recover the original array, we need to find the value of k and then reconstruct the array arr based on the values in lower and higher. We can do this by finding the minimum value in lower and the maximum value in higher, which will give us the range of values that k can take.
- Find the minimum value in
lowerand maximum value inhigher. - Calculate
kas the difference between the maximum value inhigherand the minimum value inlower. - Construct the original array
arrby addingkto each value inlower.
Time Complexity: O(n)
Space Complexity: O(n)
:
Solution Code
class Solution {
public int[] recoverArray(int[] nums) {
Arrays.sort(nums);
int n = nums.length / 2;
int k = nums[nums.length - 1] - nums[0];
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nums[i] + k;
}
return arr;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2122 (Recover the Original Array)?
This page provides optimized solutions for LeetCode problem 2122 (Recover the Original Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2122 (Recover the Original Array)?
The time complexity for LeetCode 2122 (Recover the Original Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2122 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2122 in Java, C++, or Python.