Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a positive integer n and an integer target.
Return the lexicographically smallest array of integers of size n such that:
target.n.If no such array exists, return an empty array.
A permutation of size n is a rearrangement of integers 1, 2, ..., n.
Example 1:
Input: n = 3, target = 0
Output: [-3,1,2]
Explanation:
The arrays that sum to 0 and whose absolute values form a permutation of size 3 are:
[-3, 1, 2][-3, 2, 1][-2, -1, 3][-2, 3, -1][-1, -2, 3][-1, 3, -2][1, -3, 2][1, 2, -3][2, -3, 1][2, 1, -3][3, -2, -1][3, -1, -2]The lexicographically smallest one is [-3, 1, 2].
Example 2:
Input: n = 1, target = 10000000000
Output: []
Explanation:
There are no arrays that sum to 10000000000 and whose absolute values form a permutation of size 1. Therefore, the answer is [].
Constraints:
1 <= n <= 105-1010 <= target <= 1010Problem summary: You are given a positive integer n and an integer target. Return the lexicographically smallest array of integers of size n such that: The sum of its elements equals target. The absolute values of its elements form a permutation of size n. If no such array exists, return an empty array. A permutation of size n is a rearrangement of integers 1, 2, ..., n.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Two Pointers · Greedy
3 0
1 10000000000
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3752: Lexicographically Smallest Negated Permutation that Sums to Target
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3752: Lexicographically Smallest Negated Permutation that Sums to Target
// package main
//
// // https://space.bilibili.com/206214
// func lexSmallestNegatedPerm(n int, target int64) []int {
// t := int(target)
// mx := n * (n + 1) / 2
// if t > mx || -t > mx || (mx-t)%2 != 0 {
// return nil
// }
// negS := (mx - t) / 2 // 取负号的元素(的绝对值)之和
//
// ans := make([]int, n)
// l, r := 0, n-1
// // 从 1,2,...,n 中选一些数,元素和等于 neg
// // 为了让取反后字典序尽量小,从大往小选
// for x := n; x > 0; x-- {
// if negS >= x {
// negS -= x
// ans[l] = -x
// l++
// } else {
// // 大的正数填在末尾
// ans[r] = x
// r--
// }
// }
//
// return ans
// }
// Accepted solution for LeetCode #3752: Lexicographically Smallest Negated Permutation that Sums to Target
package main
// https://space.bilibili.com/206214
func lexSmallestNegatedPerm(n int, target int64) []int {
t := int(target)
mx := n * (n + 1) / 2
if t > mx || -t > mx || (mx-t)%2 != 0 {
return nil
}
negS := (mx - t) / 2 // 取负号的元素(的绝对值)之和
ans := make([]int, n)
l, r := 0, n-1
// 从 1,2,...,n 中选一些数,元素和等于 neg
// 为了让取反后字典序尽量小,从大往小选
for x := n; x > 0; x-- {
if negS >= x {
negS -= x
ans[l] = -x
l++
} else {
// 大的正数填在末尾
ans[r] = x
r--
}
}
return ans
}
# Accepted solution for LeetCode #3752: Lexicographically Smallest Negated Permutation that Sums to Target
# Time: O(n)
# Space: O(1)
# greedy, two pointers
class Solution(object):
def lexSmallestNegatedPerm(self, n, target):
"""
:type n: int
:type target: int
:rtype: List[int]
"""
def count(x):
return (x+1)*x//2
total = count(n)
if abs(target) > total or (target-total)%2:
return []
result = [0]*n
left, right = 0, n-1
for i in reversed(xrange(1, n+1)):
if target-(-i) <= count(i-1):
target -= -i
result[left] = -i
left += 1
else:
target -= i
result[right] = i
right -= 1
return result
// Accepted solution for LeetCode #3752: Lexicographically Smallest Negated Permutation that Sums to Target
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3752: Lexicographically Smallest Negated Permutation that Sums to Target
// package main
//
// // https://space.bilibili.com/206214
// func lexSmallestNegatedPerm(n int, target int64) []int {
// t := int(target)
// mx := n * (n + 1) / 2
// if t > mx || -t > mx || (mx-t)%2 != 0 {
// return nil
// }
// negS := (mx - t) / 2 // 取负号的元素(的绝对值)之和
//
// ans := make([]int, n)
// l, r := 0, n-1
// // 从 1,2,...,n 中选一些数,元素和等于 neg
// // 为了让取反后字典序尽量小,从大往小选
// for x := n; x > 0; x-- {
// if negS >= x {
// negS -= x
// ans[l] = -x
// l++
// } else {
// // 大的正数填在末尾
// ans[r] = x
// r--
// }
// }
//
// return ans
// }
// Accepted solution for LeetCode #3752: Lexicographically Smallest Negated Permutation that Sums to Target
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3752: Lexicographically Smallest Negated Permutation that Sums to Target
// package main
//
// // https://space.bilibili.com/206214
// func lexSmallestNegatedPerm(n int, target int64) []int {
// t := int(target)
// mx := n * (n + 1) / 2
// if t > mx || -t > mx || (mx-t)%2 != 0 {
// return nil
// }
// negS := (mx - t) / 2 // 取负号的元素(的绝对值)之和
//
// ans := make([]int, n)
// l, r := 0, n-1
// // 从 1,2,...,n 中选一些数,元素和等于 neg
// // 为了让取反后字典序尽量小,从大往小选
// for x := n; x > 0; x-- {
// if negS >= x {
// negS -= x
// ans[l] = -x
// l++
// } else {
// // 大的正数填在末尾
// ans[r] = x
// r--
// }
// }
//
// return ans
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.