File tree 1 file changed +59
-0
lines changed
1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ Rearrange Array
3
+ Given two arrays of integers nums and index. Your task is to create target array under the following rules:
4
+
5
+ Initially target array is empty.
6
+ From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
7
+ Repeat the previous step until there are no elements to read in nums and index.
8
+ Return the target array.
9
+
10
+ It is guaranteed that the insertion operations will be valid.
11
+
12
+ Input
13
+ First line of the input denotes n, the size of the given arrays. The next n lines denotes the elements of
14
+ the nums array and the next n elements denotes the elements of the index array.
15
+
16
+ Output
17
+ n lines, each containing a single element from the resultant array.
18
+
19
+ Example
20
+ Input:
21
+
22
+ 5
23
+ 0
24
+ 1
25
+ 2
26
+ 3
27
+ 4
28
+ 0
29
+ 1
30
+ 2
31
+ 2
32
+ 1
33
+ Output:
34
+
35
+ 0
36
+ 4
37
+ 1
38
+ 3
39
+ 2
40
+ Explanation:
41
+ nums index target
42
+ 0 0 [0]
43
+ 1 1 [0,1]
44
+ 2 2 [0,1,2]
45
+ 3 2 [0,1,3,2]
46
+ 4 1 [0,4,1,3,2]
47
+
48
+ '''
49
+ n = int (input ())
50
+ nums = []
51
+ ind = []
52
+ target = []
53
+ for i in range (n ):
54
+ nums .append (int (input ()))
55
+ for i in range (n ):
56
+ ind .append (int (input ()))
57
+ for i in range (n ):
58
+ target .insert (ind [i ],nums [i ])
59
+ [print (target [i ]) for i in range (n )]
You can’t perform that action at this time.
0 commit comments