Similar Problems

Similar Problems not available

Find Array Given Subset Sums - Leetcode Solution

Companies:

LeetCode:  Find Array Given Subset Sums Leetcode Solution

Difficulty: Hard

Topics: array  

Problem Statement: You are given an integer array nums of length n. The problem is to generate all possible arrays of length n that contains the given array nums exactly once and has the property that the sum of the elements in the array is equal to one of the given subset sums.

Return the lexicographically smallest array among all arrays that satisfy the above conditions. A result array is lexicographically smaller than another result array if it has a smaller first element, or if the first element is the same and the second element is smaller, and so on.

Input: The input consists of two lines. The first line contains a single integer n (1 ≤ n ≤ 18), the length of nums. The second line contains n integers nums[i] (1 ≤ nums[i] ≤ 2000), the elements of nums.

Output: Return the lexicographically smallest array among all arrays that satisfy the above conditions. If there is no such array, return an empty array.

Solution: The problem requires us to find all the possible arrays containing exactly the same elements as the given array nums that have a sum equal to one of the subset sums of nums. We can solve this problem using backtracking.

We can start by generating all possible subset sums of nums using dynamic programming. We can create a boolean array dp of size (1 << n) and dp[i] will be true if there exists a subset of nums whose sum is equal to i. We can initialize dp[0] to true and for each element nums[j], we can set dp[i + nums[j]] to true for all i where dp[i] is true. Finally, we can iterate over dp and create a set of all subset sums.

We can then create an empty result array res of size n and initialize a boolean array used of size n to keep track of which elements of nums have been used in the current array. We can use backtracking to generate all possible arrays that satisfy the conditions. We can start by iterating over the possible subset sums in increasing order and for each sum s in the set, we can iterate over the elements of nums and check if the element has not already been used and if adding it to the current sum s will not exceed s. If both conditions are true, we can add the element to the result array, mark it as used and recursively call the function with the updated sum. If the length of the result array is equal to n and the current sum is equal to one of the subset sums, we have found a valid array and we can return it. Otherwise, we backtrack by removing the last added element from the result array and marking it as unused.

We can return the lexicographically smallest valid array we find. To compare two arrays, we can iterate over both of them and compare their corresponding elements. If we find an element in one of the arrays that is smaller than the corresponding element in the other array, we can stop iterating and return the smaller array.

Time Complexity: The time complexity of this solution is O(2^n * n^2) where n is the length of nums. The dynamic programming step takes O(2^n * n) time and the backtracking step takes O(2^n * n^2) time in the worst case.

Space Complexity: The space complexity of this solution is O(2^n * n) where n is the length of nums. We need an array dp of size (1 << n) for the dynamic programming step and a set of size (1 << n) to store the generated subset sums. We also need a boolean array used of size n and a result array of size n for the backtracking step.

Implementation:

Find Array Given Subset Sums Solution Code

1