Problem · Segment Tree
MediumFactSetOA

Problem statement

There is a collection of photos to place into an empty photo album, one at a time by order of importance. Each time a photo is inserted, all subsequent photos are shifted toward the right by one position. Given the id's of the photos and the positions where each should be placed, find out the sequence of the photos in the album after all photos have been inserted.

Function

photoAlbum(index: int[], identity: int[]) → int[]

Complete the function photoAlbum in the editor.

photoAlbum has the following parameter(s):

  1. int index[n]: the positions where the photos are to be inserted
  2. int identity[n]: the photograph id numbers

Returns

int[n]: the sequence of identity values after all are inserted

Input Format For Custom Testing

The first line contains an integer, n, the number of elements in the array index.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, index[i].
The next line contains an integer, n, the number of elements in the array identity.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, identity[i].

Examples

Example 1

index = [0, 1, 1]identity = [0, 1, 2]return = [0, 2, 1]
The output array goes through the following steps: [0] -> [0, 1] -> [0, 2, 1].

Example 2

index = [0, 0]identity = [0, 1]return = [1, 0]
The output array goes through the following steps: [0] -> [1, 0].

Example 3

index = [0, 1, 0]identity = [0, 1, 2]return = [2, 0, 1]
The output array goes through the following steps: [0] -> [1, 0] -> [2, 0, 1].

Constraints

  • 1 ≤ n ≤ 2 x 105
  • 0 ≤ index[i], identity[i] < n (0 ≤ i < n)
  • More FactSet problems

    drafts saved locally
    public int[] photoAlbum(int[] index, int[] identity) {
      // write your code here
    }
    
    index[0, 1, 1]
    identity[0, 1, 2]
    expected[0, 2, 1]
    checking account