Skip to main content
All Questions

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.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.