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.
- Ways to sum to N using array elements with repetition allowed
- Reorder an array according to given indexes
- Reorder an array such that sum of left half is not equal to sum of right half
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Magical Indices in an array
- Sum of all the Composite Numbers from Odd indices of the given array
- Find all good indices in the given Array
- Check if the array can be sorted using swaps between given indices only
- Reverse the substrings of the given String according to the given Array of indices
- Count of indices in an array that satisfy the given condition
- Range sum queries for anticlockwise rotations of Array by K indices
- Count the pairs in an array such that the difference between them and their indices is equal
- Find the count of unvisited indices in an infinite array
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Check if array contains contiguous integers with duplicates allowed
- Maximum sum of pairwise product in an array with negative allowed
- Find a Fixed Point in an array with duplicates allowed
- Maximum array sum with prefix and suffix multiplications with -1 allowed
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.