Advertisement

Construct Target Array With Multiple Sums - LeetCode 1354 Solution

Construct Target Array With Multiple Sums - Complete Solution Guide

Construct Target Array With Multiple Sums is LeetCode problem 1354, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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. 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 . Example 1: Input: target = [9,3,5] Output: true Explanation: Start with arr

Detailed Explanation

The problem asks whether a given array `target` of positive integers can be constructed from an initial array `arr` of the same size containing only 1s. The construction process involves repeatedly: (1) calculating the sum `x` of all elements in `arr`, (2) choosing an index `i` and setting `arr[i]` to `x`. We need to determine if the `target` array can be reached through this process.

Solution Approach

The solution uses a max heap to keep track of the largest element in the `target` array. In each iteration, it extracts the largest element and subtracts the sum of the remaining elements from it. This simulates reversing the construction process. If at any point, an invalid state is encountered (e.g., the largest element becomes less than or equal to the sum of other elements, or the remainder is 0), it means the `target` array cannot be constructed. The process continues until the largest element becomes 1, at which point we check if all other elements are also 1.

Step-by-Step Algorithm

  1. Step 1: Calculate the sum of all elements in the `target` array.
  2. Step 2: Build a max heap from the `target` array.
  3. Step 3: While the largest element in the heap is greater than 1, perform the following steps:
  4. Step 4: Extract the largest element from the heap.
  5. Step 5: Calculate the sum of the remaining elements (total_sum - largest).
  6. Step 6: Check for invalid states. If rest_sum is 0 or the largest element is less than or equal to rest_sum, return false. If rest_sum is 1, return true because all other elements must be 1.
  7. Step 7: Calculate the previous value of the largest element by taking the modulo of the largest element with the sum of the remaining elements (largest % rest_sum).
  8. Step 8: If the remainder is 0, return false because this would imply the element before the last replacement was 0, and all numbers are positive.
  9. Step 9: Update the total sum and re-insert the previous value into the max heap.
  10. Step 10: If the loop completes without returning false, it means the `target` array can be constructed, so return true.

Key Insights

  • Insight 1: Working backward is crucial. Instead of trying to build the `target` array from the initial array, it's more efficient to reduce the `target` array back to the initial array of 1s.
  • Insight 2: Using a max heap (priority queue) allows us to efficiently find the largest element in the `target` array at each step. This largest element was the last element to be modified in the construction process.
  • Insight 3: The largest element in the current `target` array must have been obtained by replacing some element with the sum of all elements in the array at the previous step. We can reverse this operation by subtracting the sum of all other elements from the largest element.

Complexity Analysis

Time Complexity: O(n + k log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Heap (Priority Queue).

Companies

Asked at: Quora.