Build Array from Permutation (LeetCode 1920): Solution and Explanation

Easy
Array
Simulation

Given a zero-based permutation nums (every integer from 0 to n - 1 exactly once), return ans where ans[i] = nums[nums[i]].

Intuition

The formula already tells you what to compute. The only thing that makes it safe is that nums is a permutation of 0..n-1, so every value is also a valid index into the same array.

Read it as two hops. From index i, the value nums[i] points at another index j. The value stored at j is the answer for i. Draw an arrow from each i to nums[i] and ans simply follows each arrow twice.

Writing into a separate array keeps the original values intact while you read them. Overwriting nums directly would break later lookups that still need the old values, which is why the in-place follow-up needs a trick.

Approach: Two lookups per index

  1. Create an array ans of length n.
  2. For each index i, read j = nums[i].
  3. Write ans[i] = nums[j].
  4. Return ans after the loop.

Step-by-step example

nums = [0,2,1,5,3,4]
  1. i = 0: nums[0] = 0, and nums[0] = 0, so ans[0] = 0.
  2. i = 1: nums[1] = 2, and nums[2] = 1, so ans[1] = 1.
  3. i = 2: nums[2] = 1, and nums[1] = 2, so ans[2] = 2.
  4. i = 3: nums[3] = 5, and nums[5] = 4, so ans[3] = 4.
  5. i = 4 gives nums[3] = 5 and i = 5 gives nums[4] = 3, so ans = [0,1,2,4,5,3].

Code: Python, Java, C++

1class Solution:
2    def buildArray(self, nums: List[int]) -> List[int]:
3        n = len(nums)
4        ans = [0] * n
5        for i in range(n):
6            j = nums[i]
7            ans[i] = nums[j]
8        return ans
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each index does two array reads and one write.

Space

O(n)

The output array has n slots. No other memory is used, and the follow-up removes even that.

Other approaches

In place with value encoding (time O(n), space O(1))

Since every value is below n, one slot can hold two numbers: set nums[i] += n * (nums[nums[i]] % n), where % n recovers the original value even if that slot was already updated. Then divide every slot by n to keep only the new value.

Common mistakes

  • Overwriting nums in place without encoding. Later indices may read a slot that already holds its new value and get the wrong answer.
  • In the encoding trick, forgetting % n when reading nums[nums[i]]. A slot that was already updated holds old + n * new, and only % n gives back the old value.
  • Mixing up the order of the two lookups. ans[i] is nums[nums[i]], not nums[i] used twice or nums[i] indexed by i again.

Frequently asked questions

Why is nums[nums[i]] always a valid index?

nums is a permutation of 0..n-1, so nums[i] is between 0 and n - 1, which is exactly the range of valid indices.

What does ans look like in terms of the permutation?

If you see nums as arrows i -> nums[i], ans is the permutation applied twice. A pair of swapped elements becomes fixed points, and a cycle of odd length stays one cycle but in a different order.

Can the in-place version overflow?

With n up to 1000, the encoded value is at most about n * n = 1,000,000, which fits in a 32-bit integer.