Skip to content

1920. Build Array from Permutation #1650

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to build an array ans from a given zero-based permutation nums such that each element ans[i] is equal to nums[nums[i]]. The solution involves iterating through the input array and constructing the result array using the specified transformation.

Approach

  1. Problem Analysis: The problem requires transforming each element of the input array nums such that each element in the resulting array ans at index i is the element in nums at the index specified by the element in nums at index i. This can be succinctly expressed as ans[i] = nums[nums[i]].
  2. Intuition: Since the input array is a zero-based permutation, each element is unique and within the valid index range. This allows us to direc…

Replies: 1 comment 2 replies

Comment options

mah-shamim
May 6, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim May 6, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants