Construct Target Array With Multiple Sums

You are given an array target of n integers. From a starting array arr consisting of n 1s, 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
Inputtarget = [9,3,5]
Outputtrue
Starting from [1, 1, 1], you can update indices to form [1, 3, 1], then [1, 3, 5], then [9, 3, 5].
Example 2
Inputtarget = [1,1,1,2]
Outputfalse
It is impossible to create the target array from [1, 1, 1, 1].

Constraints

  • n == target.length
  • 1 <= n <= 5 * 10^4
  • 1 <= target[i] <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate