Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

1389. Create Target Array in the Given Order

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

It is guaranteed that the insertion operations will be valid.

O(N^2) Time Solution

Code

class Solution{
public:
    vector<int> createTargetArray(vector<int> &nums, vector<int> &index)
    {
        vector<int> ans;
        int n = nums.size();
        for (int i = 0; i < n; i++)
            ans.insert(ans.begin() + index[i], nums[i]);
        return ans;
    }
};

O(NlogN) based on β€œsmaller elements after self”.