Compose a Zero-Based Permutation with Itself

Quick Overview

Compose a Zero-Based Permutation with Itself. The array nums contains each integer from zero through nums.length-1 exactly once. Return an array whose entry at i is the original nums[nums[i]]. The follow-up asks for constant auxiliary space.

Compose a Zero-Based Permutation with Itself

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given a zero-based permutation `nums` of length `n`, return an array `ans` of the same length satisfying `ans[i] = nums[nums[i]]` for every index. Both lookups refer to the original permutation. The source follow-up asks for an approach using `O(1)` auxiliary memory. ```cpp class Solution { public: vector<int> buildArray(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [1,2,0] Output: [2,0,1] Explanation: Looking up indices 1, 2, 0 yields 2, 0, 1. ``` **Example 2:** ```text nums = [2,1,0,3] Output: [0,1,2,3] Explanation: Composing these two swaps and fixed points gives the identity. ``` ### Constraints - `1 <= nums.length <= 1000`. - Entries are distinct and lie in `[0,nums.length-1]`. ### Clarifying Questions Both lookups use original values. The source does not require preserving the input array. ```hint Inspect the structure Can one integer store both an old value and a new value when both are smaller than n? ``` ### Reasoning checkpoints - Produce every requested double lookup using original values, even when storage is reused. - For the source space follow-up, justify the memory accounting and the intermediate integer range. ### Optional extensions The source follow-up asks whether the result can be produced with `O(1)` auxiliary memory; explain which storage is excluded as output.

Overview: Compose a Zero-Based Permutation with Itself. The array nums contains each integer from zero through nums.length-1 exactly once. Return an array whose entry at i is the original nums[nums[i]]. The follow-up asks for constant auxiliary space.

Amazon logo
Amazon
May 28, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given a zero-based permutation nums of length n, return an array ans of the same length satisfying ans[i] = nums[nums[i]] for every index. Both lookups refer to the original permutation. The source follow-up asks for an approach using O(1) auxiliary memory.

class Solution {
public:
    vector<int> buildArray(vector<int>& nums);
};

Examples

Example 1:

nums = [1,2,0]
Output: [2,0,1]
Explanation: Looking up indices 1, 2, 0 yields 2, 0, 1.

Example 2:

nums = [2,1,0,3]
Output: [0,1,2,3]
Explanation: Composing these two swaps and fixed points gives the identity.

Constraints

  • 1 <= nums.length <= 1000 .
  • Entries are distinct and lie in [0,nums.length-1] .

Clarifying Questions Guidance

Both lookups use original values. The source does not require preserving the input array.

Reasoning checkpoints

  • Produce every requested double lookup using original values, even when storage is reused.
  • For the source space follow-up, justify the memory accounting and the intermediate integer range.

Optional extensions

The source follow-up asks whether the result can be produced with O(1) auxiliary memory; explain which storage is excluded as output.