Build Array from Permutation (LeetCode 1920): Solution and Explanation
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
- Create an array
ansof length n. - For each index
i, readj = nums[i]. - Write
ans[i] = nums[j]. - Return
ansafter the loop.
Step-by-step example
nums = [0,2,1,5,3,4]
- i = 0: nums[0] = 0, and nums[0] = 0, so ans[0] = 0.
- i = 1: nums[1] = 2, and nums[2] = 1, so ans[1] = 1.
- i = 2: nums[2] = 1, and nums[1] = 2, so ans[2] = 2.
- i = 3: nums[3] = 5, and nums[5] = 4, so ans[3] = 4.
- 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 ans1class Solution {
2 public int[] buildArray(int[] nums) {
3 int n = nums.length;
4 int[] ans = new int[n];
5 for (int i = 0; i < n; i++) {
6 int j = nums[i];
7 ans[i] = nums[j];
8 }
9 return ans;
10 }
11}1class Solution {
2public:
3 vector<int> buildArray(vector<int>& nums) {
4 int n = nums.size();
5 vector<int> ans(n);
6 for (int i = 0; i < n; i++) {
7 int j = nums[i];
8 ans[i] = nums[j];
9 }
10 return ans;
11 }
12};Time and space complexity
O(n)
Each index does two array reads and one write.
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
% nwhen reading nums[nums[i]]. A slot that was already updated holds old + n * new, and only% ngives 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.