Construct Target Array With Multiple Sums - Problem
You are given an array target of n integers. From a starting array arr consisting of n 1's, you may perform the following procedure:
- Let
xbe the sum of all elements currently in your array. - Choose index
i, such that0 <= i < nand set the value ofarrat indexitox.
You may repeat this procedure as many times as needed.
Return true if it is possible to construct the target array from arr, otherwise, return false.
Input & Output
Example 1 — Basic Possible Case
$
Input:
target = [9,3,5]
›
Output:
true
💡 Note:
Starting from [1,1,1]: Replace index 0 with sum=3 → [3,1,1], then replace index 2 with sum=5 → [3,1,5], finally replace index 0 with sum=9 → [9,3,5]. Working backwards confirms this is possible.
Example 2 — Impossible Case
$
Input:
target = [1,1,1,2]
›
Output:
false
💡 Note:
The sum is 5. For 2 to be created, the previous sum must have been 2, but that would require other elements to sum to 0, which is impossible since all elements must be positive.
Example 3 — Single Element
$
Input:
target = [8]
›
Output:
true
💡 Note:
Single element case: start with [1], replace with 1 → [1], replace with 1 → [1], ..., eventually we can build up to any positive integer.
Constraints
- 1 ≤ target.length ≤ 5 × 104
- 1 ≤ target[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Start
Begin with array [1,1,1]
2
Transform
Replace elements with current sum
3
Target
Check if we can reach target [9,3,5]
Key Takeaway
🎯 Key Insight: Work backwards from the target - the largest element reveals the previous state
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code