# Leetcode 1389: Create Target Array in the Given Order

Category: Easy

## Problem

Given two arrays of integers `nums` and `index`. Your task is to create target array under the following rules:

• Initially target array is empty.
• From left to right read nums[i] and index[i], insert at index `index[i]` the value `nums[i]` in target array.
• Repeat the previous step until there are no elements to read in `nums` and `index.`

Return the target array.

It is guaranteed that the insertion operations will be valid.

https://leetcode.com/problems/create-target-array-in-the-given-order/

## Constraints

• `1 <= nums.length, index.length <= 100`
• `nums.length == index.length`
• `0 <= nums[i] <= 100`
• `0 <= index[i] <= i`

## Solution Approach

In this problem, we have to have an array whose values are given in another array and what value goes to what index is also provided just that an index can have many values and we have to solve that.

We can use ArrayList to do just that we will populate it using the add method at the given index and value. Add method has 2 signatures we will use the one where we can input the value as well as an index in the ArrayList. In the end, we just convert that ArrayList to an integer array and send the value as the answer.

Coming to the coding part if the length of the index array is 1 then we straightaway return the same nums array. After that, we create an array which will contain the actual array which we send from the method and an Arraylist to temporarily store the index and value. Next, to the ArrayList, we add the values of the nums array at the index’s index using the add method. Then we convert the ArrayList and its elements from Object to integer in the solution array and return the same.

## Solution code

For more Leetcode explained solutions visit Leetcode Solutions. If you like capture the flag challenges visit here.

Check out my socials below in the footer. Feel free to ask any doubts in the comment section or contact me via Contact page I will surely respond. Happy Leetcoding 🙂