File: build-array-from-permutation/solution.py

Date: 2026-06-06

Time: 15:28

Build Array from Permutation — solution.py

Purpose

Solves LeetCode 1920: Build Array from Permutation. Given a zero-based permutation nums, construct an array ans where ans[i] = nums[nums[i]]. The solution does this in-place using O(1) extra space, which is the follow-up challenge — the naive approach would just allocate a new array.

Key Components

Solution.buildArray(nums: List[int]) -> List[int] — The only method. Mutates nums in place and returns it. Takes a permutation (every value in [0, n) appears exactly once) and replaces each element with the doubly-dereferenced value.

Patterns

The core technique is encoding two values in one integer using modular arithmetic. Since every value is in [0, n), each slot can store both its original value and its new value simultaneously:


encoded = original + n * new_value

This is a standard in-place permutation trick that avoids the chicken-and-egg problem: when you overwrite nums[i], you'd lose the original value that some later nums[j] might need. By packing both into one integer, nothing is lost.

Two-pass structure:

1. Encode pass (line 14): For each i, compute the new value as nums[nums[i]] % n (using % n because nums[nums[i]] might already be encoded from an earlier iteration), multiply by n, and add it to nums[i].

2. Decode pass (line 17): Integer-divide each element by n to extract just the new value, discarding the original.

Dependencies

Flow

Given nums = [0, 2, 1, 5, 3, 4] (n=6):

| i | original nums[i] | nums[nums[i]] % n | encoded value |

|---|---|---|---|

| 0 | 0 | 0 | 0 + 6*0 = 0 |

| 1 | 2 | 1 | 2 + 6*1 = 8 |

| 2 | 1 | 8%6=2 | 1 + 6*2 = 13 |

| 3 | 5 | 4 | 5 + 6*4 = 29 |

| 4 | 3 | 29%6=5 | 3 + 6*5 = 33 |

| 5 | 4 | 33%6=3 | 4 + 6*3 = 22 |

After decode (//6): [0, 1, 2, 4, 5, 3] — which is [nums[nums[i]]] for each i.

Invariants

Error Handling

None. The method trusts that the caller provides a valid permutation per the LeetCode contract. No bounds checking, no validation.

Topics to Explore

Beliefs