|
| 1 | +# Construct Target Array With Multiple Sums |
| 2 | + |
| 3 | +You are given an array `target` of `n` integers. From a starting array `arr` consisting of `n` 1's, you may perform the |
| 4 | +following procedure : |
| 5 | + |
| 6 | +- let x be the sum of all elements currently in your array. |
| 7 | +- choose index i, such that 0 <= i < n and set the value of arr at index i to x. |
| 8 | +- You may repeat this procedure as many times as needed. |
| 9 | + |
| 10 | +Return true if it is possible to construct the target array from arr, otherwise, return false. |
| 11 | + |
| 12 | +## Examples |
| 13 | + |
| 14 | +Example 1: |
| 15 | + |
| 16 | +```text |
| 17 | +Input: target = [9,3,5] |
| 18 | +Output: true |
| 19 | +Explanation: Start with arr = [1, 1, 1] |
| 20 | +[1, 1, 1], sum = 3 choose index 1 |
| 21 | +[1, 3, 1], sum = 5 choose index 2 |
| 22 | +[1, 3, 5], sum = 9 choose index 0 |
| 23 | +[9, 3, 5] Done |
| 24 | +``` |
| 25 | + |
| 26 | +Example 2: |
| 27 | + |
| 28 | +```text |
| 29 | +Input: target = [1,1,1,2] |
| 30 | +Output: false |
| 31 | +Explanation: Impossible to create target array from [1,1,1,1]. |
| 32 | +``` |
| 33 | + |
| 34 | +Example 3: |
| 35 | + |
| 36 | +```text |
| 37 | +Input: target = [8,5] |
| 38 | +Output: true |
| 39 | +``` |
| 40 | + |
| 41 | +## Constraints |
| 42 | + |
| 43 | +- n == target.length |
| 44 | +- 1 <= n <= 5 * 10^4 |
| 45 | +- 1 <= target[i] <= 10^9 |
| 46 | + |
| 47 | +## Topics |
| 48 | + |
| 49 | +- Array |
| 50 | +- Heap (Priority Queue) |
| 51 | + |
| 52 | +## Hints |
| 53 | + |
| 54 | +- Given that the sum is strictly increasing, the largest element in the target must be formed in the last step by adding |
| 55 | + the total sum in the previous step. Thus, we can simulate the process in a reversed way. |
| 56 | +- Subtract the largest with the rest of the array, and put the new element into the array. Repeat until all elements |
| 57 | + become one |
| 58 | + |
| 59 | +## Solution |
| 60 | + |
| 61 | +The straightforward approach starts with an array of all 1’s and replaces one element with the sum of all elements, |
| 62 | +repeating this process. For example, starting with [1, 1, 1, 1], the sum is 4, and one number is replaced with this sum, |
| 63 | +leading to arrays like [4, 1, 1, 1], [1, 4, 1, 1], and so on. This process grows exponentially as the number of possible |
| 64 | +paths increases, making it computationally expensive for larger arrays. The expanding sum makes it hard to predict the |
| 65 | +correct path, and this approach struggles with large arrays due to its high computational cost and inefficiency. |
| 66 | + |
| 67 | +The optimized approach works in reverse, starting from the target array and moving backward toward an array of all 1’s. |
| 68 | +This method reduces the number of possibilities by eliminating unnecessary branching. Instead of growing the array toward |
| 69 | +the target, the algorithm reduces the target by calculating the previous values. At each step, it identifies the largest |
| 70 | +number in the array and computes the sum of the remaining elements. |
| 71 | + |
| 72 | +Instead of directly subtracting the largest number from the sum, the algorithm uses the modulo operation to find the |
| 73 | +previous value by computing the largest number modulo the sum of the other elements. This step-by-step reduction mimics |
| 74 | +working backward through the array, simplifying the array structure. A max heap ensures that the largest number is always |
| 75 | +processed first. This is crucial, as reducing the largest number drives the process toward convergence. The modulo |
| 76 | +operation progressively reduces the largest element, avoiding unnecessary branching and computations. The algorithm |
| 77 | +continues reducing until it reaches an array of all 1’s, confirming the target is achievable, or detects an invalid state, |
| 78 | +signaling failure. This optimized approach handles larger arrays effectively, reducing the largest number step-by-step and |
| 79 | +making the process computationally efficient and scalable. By working backward and minimizing the search space through |
| 80 | +subtraction and modulo, the algorithm ensures a unique path to the target, making it faster and more efficient. |
| 81 | + |
| 82 | +The steps of the algorithm are as follows: |
| 83 | + |
| 84 | +1. Initialize a max-heap and push all elements of the target array into the heap. |
| 85 | +2. Calculate the total_sum of the target array. |
| 86 | +3. Iterate while the max-heap is not empty: |
| 87 | + - Pop the largest element from the heap (current_max). |
| 88 | + - Compute the sum of the remaining elements: remaining_sum = total_sum - current_max. |
| 89 | + - Check for base cases: |
| 90 | + - If current_max == 1 or remaining_sum == 1, return True, because we can construct the array. |
| 91 | + - If remaining_sum == 0, or current_max < remaining_sum, or current_max % remaining_sum == 0, return False — it’s |
| 92 | + invalid or stuck in an infinite loop. |
| 93 | + - Simulate the reverse of the operation: |
| 94 | + - Compute the previous value before current_max was formed: updated_value = current_max % remaining_sum. |
| 95 | + - Update the total sum: total_sum = remaining_sum + updated_value. |
| 96 | + - Push updated_value back into the heap. |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +### Time Complexity |
| 116 | + |
| 117 | +The time complexity of the solution is O(nlog(n)) because each heap operation (push and pop) takes log(n) time and we |
| 118 | +may perform it up to O(n) times in the worst case. |
| 119 | + |
| 120 | +### Space Complexity |
| 121 | + |
| 122 | +The space complexity of the above solution is O(n), for storing the heap. |
0 commit comments