Construct the lexicographically smallest signed permutation with a given sum.
Unlock detailed company stats for this questionUpgrade
Given integers size (n) and target_sum, construct an integer array arr of length size such that the sum of its elements equals target_sum and the absolute values of its elements form a permutation of {1, 2, ..., size}. Among all valid arrays return the lexicographically smallest one (decided by the first differing index). If it is not possible, return an array of size size filled with 0. Example 1: size = 5, target_sum = 9. Valid: [-1, -2, 3, 4, 5] and [-3, 1, 2, 4, 5]; output [-3, 1, 2, 4, 5]. Example 2: size = 4, target_sum = -2 gives [-4, -2, 1, 3]. Example 3: size = 3, target_sum = 5 gives [0, 0, 0].
Community answers
No answers contributed by the community yet.


