Skip to content

Commit 383ccd4

Browse files
committed
2122. Recover the Original Array: RETRY
1 parent 039c194 commit 383ccd4

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,3 +1602,4 @@ mod s2117_abbreviating_the_product_of_a_range;
16021602
mod s2119_a_number_after_a_double_reversal;
16031603
mod s2120_execution_of_all_suffix_instructions_staying_in_a_grid;
16041604
mod s2121_intervals_between_identical_elements;
1605+
mod s2122_recover_the_original_array;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* [2122] Recover the Original Array
3+
*
4+
* 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:
5+
* <ol>
6+
* lower[i] = arr[i] - k, for every index i where 0 <= i < n
7+
* higher[i] = arr[i] + k, for every index i where 0 <= i < n
8+
* </ol>
9+
* 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.
10+
* 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.
11+
* Note: The test cases are generated such that there exists at least one valid array arr.
12+
*
13+
* Example 1:
14+
*
15+
* Input: nums = [2,10,6,4,8,12]
16+
* Output: [3,7,11]
17+
* Explanation:
18+
* If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12].
19+
* Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums.
20+
* Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12].
21+
*
22+
* Example 2:
23+
*
24+
* Input: nums = [1,1,3,3]
25+
* Output: [2,2]
26+
* Explanation:
27+
* If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3].
28+
* Combining lower and higher gives us [1,1,3,3], which is equal to nums.
29+
* 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.
30+
* This is invalid since k must be positive.
31+
*
32+
* Example 3:
33+
*
34+
* Input: nums = [5,435]
35+
* Output: [220]
36+
* Explanation:
37+
* The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].
38+
*
39+
*
40+
* Constraints:
41+
*
42+
* 2 * n == nums.length
43+
* 1 <= n <= 1000
44+
* 1 <= nums[i] <= 10^9
45+
* The test cases are generated such that there exists at least one valid array arr.
46+
*
47+
*/
48+
pub struct Solution {}
49+
50+
// problem: https://leetcode.com/problems/recover-the-original-array/
51+
// discuss: https://leetcode.com/problems/recover-the-original-array/discuss/?currentPage=1&orderBy=most_votes&query=
52+
53+
// submission codes start here
54+
55+
impl Solution {
56+
pub fn recover_array(nums: Vec<i32>) -> Vec<i32> {
57+
vec![]
58+
}
59+
}
60+
61+
// submission codes end
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
#[ignore]
69+
fn test_2122_example_1() {
70+
let nums = vec![2, 10, 6, 4, 8, 12];
71+
72+
let result = vec![3, 7, 11];
73+
74+
assert_eq!(Solution::recover_array(nums), result);
75+
}
76+
77+
#[test]
78+
#[ignore]
79+
fn test_2122_example_2() {
80+
let nums = vec![1, 1, 3, 3];
81+
82+
let result = vec![2, 2];
83+
84+
assert_eq!(Solution::recover_array(nums), result);
85+
}
86+
87+
#[test]
88+
#[ignore]
89+
fn test_2122_example_3() {
90+
let nums = vec![5, 435];
91+
92+
let result = vec![220];
93+
94+
assert_eq!(Solution::recover_array(nums), result);
95+
}
96+
}

0 commit comments

Comments
 (0)