Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1354: Construct Target Array With Multiple Sums

In this guide, we solve Leetcode #1354 Construct Target Array With Multiple Sums in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Array, Heap (Priority Queue)

Intuition

We need to repeatedly access the smallest or largest element as the input changes.

A heap provides fast insertions and removals while keeping order.

Approach

Push candidates into the heap as you scan, and pop when you need the best element.

Keep the heap size bounded if the problem requires a top-k structure.

Steps:

  • Push candidates into a heap.
  • Pop the best candidate when needed.
  • Maintain heap size or invariants.

Example

Input: target = [9,3,5] Output: true Explanation: Start with arr = [1, 1, 1] [1, 1, 1], sum = 3 choose index 1 [1, 3, 1], sum = 5 choose index 2 [1, 3, 5], sum = 9 choose index 0 [9, 3, 5] Done

Python Solution

class Solution: def isPossible(self, target: List[int]) -> bool: s = sum(target) pq = [-x for x in target] heapify(pq) while -pq[0] > 1: mx = -heappop(pq) t = s - mx if t == 0 or mx - t < 1: return False x = (mx % t) or t heappush(pq, -x) s = s - mx + x return True

Complexity

The time complexity is O(nlog⁡n)O(n \log n)O(nlogn) and the space complexity is O(n)O(n)O(n), where nnn is the length of array target\textit{target}target. The space complexity is O(n)O(n)O(n), where nnn is the length of array target\textit{target}target.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy