# Solution For Build Array From Permutation

Problem:
Given a zero-based permutation nums, build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.

A zero-based permutation nums is an array of distinct integers from 0 to nums.length – 1 (inclusive).

Solution:
To solve this problem, we need to iterate over the given permutation array nums and create a new array ans such that ans[i] = nums[nums[i]] for each i from 0 to nums.length – 1.

Algorithm:
1. Initialize an empty array ans of the same length as nums.
2. Iterate over nums from 0 to n-1 and set ans[i] = nums[nums[i]].

Let’s look at the implementation of this algorithm in Python:

“`
def buildArray(nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * n

for i in range(n):
ans[i] = nums[nums[i]]

return ans

“`

In the above code, we first initialized an empty array ans of the same length as nums, and then we iterated over nums from 0 to n-1 and set ans[i] = nums[nums[i]].

Time Complexity: O(n)
Space Complexity: O(n)

Let’s look at an example to understand this solution better:

Example:
nums = [0, 2, 1, 5, 3, 4] n = 6

Initially, ans = [0, 0, 0, 0, 0, 0]

At i = 0, ans[0] = nums[nums[0]] = nums[0] = 0, ans = [0, 0, 0, 0, 0, 0] At i = 1, ans[1] = nums[nums[1]] = nums[1] = 1, ans = [0, 1, 0, 0, 0, 0] At i = 2, ans[2] = nums[nums[2]] = nums[2] = 2, ans = [0, 1, 2, 0, 0, 0] At i = 3, ans[3] = nums[nums[3]] = nums[4] = 3, ans = [0, 1, 2, 3, 0, 0] At i = 4, ans[4] = nums[nums[4]] = nums[3] = 5, ans = [0, 1, 2, 3, 5, 0] At i = 5, ans[5] = nums[nums[5]] = nums[5] = 4, ans = [0, 1, 2, 3, 5, 4]

Hence the final output is [0, 1, 2, 3, 5, 4].

## Step by Step Implementation For Build Array From Permutation

public int[] buildArray(int[] target, int n) {
// create an array to store the result
int[] result = new int[target.length + n - 1];

// fill in the first n-1 elements with 1 to n
for (int i = 0; i < n-1; i++) {
result[i] = i+1;
}

// fill in the remaining elements with the target array
for (int i = 0; i < target.length; i++) {
result[i+n-1] = target[i];
}

return result;
}
Given the array of integers nums and the array of requests requests where requests[i] = [starti, endi], your task is to return the number of times you must build the array.

If there are multiple valid solutions, return any of them. It is guaranteed that there will be at least one valid solution.

def buildArray(nums, requests):

# initialize a variable to track the number of times we build the array
num_builds = 0

# iterate through the requests
for request in requests:

# unpack the start and end indices
start, end = request

# build the array for this request
for i in range(start, end + 1):
num_builds += 1
print(i)

# return the number of builds
return num_builds
/**
* @param {number[]} permutation
* @return {number[][]}
*/
var buildArray = function(permutation) {
// create an empty results array
let results = [];

// iterate through the permutation array
for (let i = 0; i < permutation.length; i++) {
// create a new array consisting of the numbers 1 through permutation[i]
let newArray = [];
for (let j = 1; j <= permutation[i]; j++) {
newArray.push(j);
}
// add the new array to the results array
results.push(newArray);
}

return results;
};
vector buildArray(vector& target, int n) {
vector res;
// maintain a hash set to keep track of what numbers have been added to the result so far
unordered_set set;
// iterate through the target array
for (int i = 0; i < target.size(); i++) {
// add the current number to the result
res.push_back(target[i]);
// add the current number to the hash set
set.insert(target[i]);
// if the current number is not the last number in the target array, we need to add n - target[i] to the result
if (i != target.size() - 1) {
for (int j = 1; j < n - target[i]; j++) {
// if the number n - j has not been added to the result yet, add it
if (!set.count(n - j)) {
res.push_back(n - j);
set.insert(n - j);
}
}
}
}
return res;
}
public int[] BuildArray(int[] target, int n) {

// create an array to store the result
int[] result = new int[target.Length + n - 1];

// set the first element in the array to 1
result[0] = 1;

// initialize j to 1 (the second element in the array)
int j = 1;

// loop through each element in the target array
for (int i = 0; i < target.Length; i++) {

// set the current element in the result array to the value in the target array
result[j] = target[i];

// if the current element in the target array is not equal to the previous element
if (i > 0 && target[i] != target[i-1]) {

// fill the remaining elements in the result array with the value of the previous element in the target array
for (int k = j + 1; k < j + n; k++) {
result[k] = target[i-1];
}
}

// increment j
j++;
}

// return the result array
return result;
}

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]