Construct Target Array With Multiple Sums - Problem
Construct Target Array With Multiple Sums

Imagine you're a mathematician trying to reverse-engineer a sequence! You start with an array arr of n ones: [1, 1, 1, ..., 1]. Your goal is to transform this humble beginning into a given target array through a series of strategic operations.

The Rule: In each step, you can:
1️⃣ Calculate the sum x of all current elements
2️⃣ Pick any index i and replace arr[i] with x

For example, starting with [1, 1, 1]:
• Sum = 3, replace index 0 → [3, 1, 1]
• Sum = 5, replace index 1 → [3, 5, 1]
• Sum = 9, replace index 2 → [3, 5, 9]

Your Mission: Determine if it's possible to construct the given target array using these operations. Return true if possible, false otherwise.

This problem tests your ability to work backwards and understand mathematical relationships!

Input & Output

example_1.py — Python
$ Input: [9,3,5]
Output: true
💡 Note: Starting from [1,1,1]: sum=3 → [3,1,1], sum=5 → [3,1,5], sum=9 → [3,9,5], sum=17 → [3,5,17]. Wait, this doesn't work. Let's reverse engineer: [9,3,5] → max=9, others=8, previous=1 → [1,3,5] → max=5, others=4, previous=1 → [1,3,1] → max=3, others=2, previous=1 → [1,1,1] ✓
example_2.py — Python
$ Input: [1,1,1,2]
Output: true
💡 Note: Working backwards: [1,1,1,2] → max=2, others=3, previous=-1. This is negative, but we need to check if 2 could come from 1+1+1+1=4 total, so 2 was created when sum was 2+others, meaning 2=sum, so others=0. Actually, let's trace forward: [1,1,1,1] sum=4 → [4,1,1,1] sum=7 → [4,1,1,7] sum=13 → ... This seems impossible. Let me recalculate: if target is [1,1,1,2], the max is 2, others sum to 3, so previous would be 2-3=-1 which is impossible.
example_3.py — Python
$ Input: [8,5]
Output: true
💡 Note: Reverse engineering: [8,5] → max=8, others=5, previous=8-5=3 → [3,5] → max=5, others=3, previous=5-3=2 → [3,2] → max=3, others=2, previous=3-2=1 → [1,2] → max=2, others=1, previous=2-1=1 → [1,1] ✓

Constraints

  • 1 ≤ target.length ≤ 5 × 104
  • 1 ≤ target[i] ≤ 109
  • All elements in target are positive integers

Visualization

Tap to expand
🔍 Reverse Engineering: Archaeological Approach[9, 3, 5]Target StateMax: 9[1, 3, 5]Previous State9 - (3+5) = 1 ✓[1, 1, 5]Earlier State5 - (1+1) = 3 ✓[1, 1, 1]Base CaseSuccess! ✓🧮 Mathematical Formulaprevious_value = current_max - sum_of_othersIf previous_value < 1 → ImpossibleIf previous_value ≥ 1 → Continue process✅ Why This Works• Largest element was modified last• Can reverse each step uniquely• Much faster than forward search• Uses heap for efficiencyTime: O(n + log(max) × log n)❌ Impossible Cases• previous_value < 1• sum_of_others ≥ current_max• Single element ≠ 1Example: [4,2,3]4 - (2+3) = -1 < 1 ✗
Understanding the Visualization
1
Identify Last Change
The largest element must have been modified most recently
2
Calculate Previous State
Use math: previous_value = current_max - sum_of_others
3
Validate & Continue
If previous_value < 1, impossible. Otherwise, repeat process
4
Reach Base Case
Success when all elements become 1
Key Takeaway
🎯 Key Insight: The largest element in the current state must have been the most recently modified element, allowing us to reverse-engineer the construction process efficiently using mathematical relationships rather than brute force exploration.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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