Photo Album
Problem statement
An album starts empty. You are given two integer arrays, index and identity, each of length n.
Process the photos in input order. At step i, insert the photo with ID identity[i] at zero-based position index[i] in the current album. Shift every photo at that position or farther right one position to the right.
index[i] = 0inserts at the beginning.index[i] = iappends after all photos already inserted.- Photo IDs are values, not insertion positions. Repeated IDs are allowed and represent separate photo entries.
Return the final left-to-right sequence of photo IDs after all n insertions.
Function
photoAlbum(index: int[], identity: int[]) → int[]Examples
Example 1
index = [0,1,1,0]identity = [20,30,40,10]return = [10,20,40,30]The album changes from [] to [20], then [20,30], then [20,40,30], and finally [10,20,40,30].
Example 2
index = [0,0,0,0]identity = [7,7,2,9]return = [9,2,7,7]Every photo is inserted at the front, reversing the insertion order. Both entries whose ID is 7 remain in the album.
Example 3
index = [0,1,2]identity = [5,3,2]return = [5,3,2]Each photo is appended, so the final order equals the input ID order.
Constraints
1 <= n <= 2 * 10^5.index.length = identity.length = n.0 <= index[i] <= ifor every0 <= i < n.0 <= identity[i] <= 10^9. IDs need not be distinct.