1982. Find Array Given Subset Sums Hard

@problem@discussion
#Array#Divide and Conquer



1/**
2 * [1982] Find Array Given Subset Sums
3 *
4 * You are given an integer n representing the length of an unknown array that you are trying to recover. You are also given an array sums containing the values of all 2^n subset sums of the unknown array (in no particular order).
5 * Return the array ans of length n representing the unknown array. If multiple answers exist, return any of them.
6 * An array sub is a subset of an array arr if sub can be obtained from arr by deleting some (possibly zero or all) elements of arr. The sum of the elements in sub is one possible subset sum of arr. The sum of an empty array is considered to be 0.
7 * Note: Test cases are generated such that there will always be at least one correct answer.
8 *  
9 * Example 1:
10 * 
11 * Input: n = 3, sums = [-3,-2,-1,0,0,1,2,3]
12 * Output: [1,2,-3]
13 * Explanation: [1,2,-3] is able to achieve the given subset sums:
14 * - []: sum is 0
15 * - [1]: sum is 1
16 * - [2]: sum is 2
17 * - [1,2]: sum is 3
18 * - [-3]: sum is -3
19 * - [1,-3]: sum is -2
20 * - [2,-3]: sum is -1
21 * - [1,2,-3]: sum is 0
22 * Note that any permutation of [1,2,-3] and also any permutation of [-1,-2,3] will also be accepted.
23 * 
24 * Example 2:
25 * 
26 * Input: n = 2, sums = [0,0,0,0]
27 * Output: [0,0]
28 * Explanation: The only correct answer is [0,0].
29 * 
30 * Example 3:
31 * 
32 * Input: n = 4, sums = [0,0,5,5,4,-1,4,9,9,-1,4,3,4,8,3,8]
33 * Output: [0,-1,4,5]
34 * Explanation: [0,-1,4,5] is able to achieve the given subset sums.
35 * 
36 *  
37 * Constraints:
38 * 
39 * 	1 <= n <= 15
40 * 	sums.length == 2^n
41 * 	-10^4 <= sums[i] <= 10^4
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/find-array-given-subset-sums/
47// discuss: https://leetcode.com/problems/find-array-given-subset-sums/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn recover_array(n: i32, sums: Vec<i32>) -> Vec<i32> {
53        vec![]
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_1982() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.