Given two integer arrays arr and index of size N, the task is to create a new array by inserting the elements given in the arr array at the indices given by the index array. If a particular position occur multiple time then right shift the right-side array elements and then insert element at given index.
Note: It is given that all the indices that in the index array lie in the range [0, N).
Input: arr = [0, 1, 2, 3, 4], index = [0, 1, 2, 2, 1]
Output: [0, 4, 1, 3, 2]
First we insert at 0th, 1st and 2nd index so the array will become [0, 1, 2]
Then we insert again at 2nd position and rightshift all rightside elements so array will be [0, 1, 3, 2]
Then we insert 4 at first index so final array will become: [0, 4, 1, 3, 2].
Input: arr = [1, 2, 3, 4, 0], index = [0, 1, 2, 3, 0]
Output: [0, 1, 2, 3, 4]
Approach (Using static array):
If we use a static array, then the given problem can be solved using the following steps:
- Create a new array finalArr of size N, to store the resultant output.
- For each element in the given arr array, insert it at the corresponding given index given by the index array, simply using:
finalArr[index[i]] = arr[i] where i is the current position during iteration
- If the index is repeated, then right shift all right-side element and then finally insert at that position.
Approach (Using dynamic array):
If we use a dynamic array-like structure, like Vectors, etc; then we can simply insert the given element at the given index, without worrying about the repetition. The vector-like data structure takes care of the right shifting by itself, when it expands at each insertion.
- Create a new vector vec, to store the resultant output.
- For each element in the given arr array, insert it at the corresponding given index given by the index array, simply using insert() function of vector. This will insert at a particular position, and take care of repeating positions automatically.
Below is the implementation of the above approach:
0 4 1 3 2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Reorder an array according to given indexes
- Ways to sum to N using array elements with repetition allowed
- Reverse the substrings of the given String according to the given Array of indices
- Rearrange a string according to the given indices
- Reorder an array such that sum of left half is not equal to sum of right half
- Reorder the position of the words in alphabetical order
- Smallest subarray which upon repetition gives the original array
- Longest chain of arr[i], arr[arr[i]], .. without repetition
- Find all good indices in the given Array
- Check if the array can be sorted using swaps between given indices only
- Count of indices in an array that satisfy the given condition
- Sum of all the Composite Numbers from Odd indices of the given array
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Sort an array according to absolute difference with given value
- Check loop in array according to given constraints
- Split the array into equal sum parts according to given conditions
- Sort an array according to absolute difference with given value using Functors
- Minimize the sum of the array according the given condition
- Generate an array of size N according to the given rules
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.