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 x be the sum of all elements currently in your array.
  • Choose index i, such that 0 <= i < n and set the value of arr at index i to x.

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
Construct Target Array: [1,1,1] → [9,3,5]111sum = 3311sum = 5315sum = 9935Replace elementwith current sumTarget [9,3,5] achieved!
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
Asked in
Google 15 Amazon 12 Facebook 8
32.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen