-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1_right_shift.py
103 lines (68 loc) · 1.4 KB
/
1_right_shift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
# N = 4
# idx 0 1 2 3
# A = [1, 2, 3, 4]
# Output:
# 4 1 2 3
# -------------------------------
x = A[N - 1]
# x = 4
# [1, 2, 3, 4]
# -------------------------------
A[N - 1] = A[N - 2] # A[3] = A[2]
# A = [1, 2, 3, 3]
# -------------------------------
A[N - 2] = A[N - 3] # A[2] = A[1]
# idx: 0 1 2 3
# A = [1, 2, 3, 3]
# A[2] = A[1]
# A = [1, 2, 2, 3]
# -------------------------------
A[N - 3] = A[N - 4] # A[1] = A[0]
# A = [1, 1, 2, 3]
# O = [4, 1, 2, 3]
# -------------------------------
A[0] = x
# A = [4, 1, 2, 3]
# -------------------------------
x = 5
1 2 3 4 5
Shift 4 to the right
1 2 3 4 4
Shift 3 to the right
1 2 3 3 4
Shift 2 to the right
1 2 2 3 4
Shift 1 to the right
1 1 2 3 4
Place 5 at it's position, index 0
5 1 2 3 4
"""
# N = 5
# A = [1, 2, 3, 4, 5]
# -------------------
# x = A[N - 1]
# -------------------
# A[N - 1] = A[N - 2] => A[4] = A[3]
# A[N - 2] = A[N - 3] => A[3] = A[2]
# A[N - 3] = A[N - 4] => A[2] = A[1]
# A[N - 4] = A[N - 5] => A[1] = A[0]
# -------------------
# generalize the loop part:
# A[i] = A[i - 1], i = N - 1, N - 2, N - 3, N - 4
# start from i = N -1
# go till i = 1
# range(N - 1, 0, -1) => [N - 1, N - 2, ..., 1]
# -------------------
# A[0] = x
# -------------------
N = int(input())
A = list(map(int, input().split()))
x = A[N - 1]
i = N - 1
while i >= 1:
A[i] = A[i - 1]
i -= 1
A[0] = x
for y in A:
print(y, end=' ')