blob: 960f706d017ab2f1949dd473bf07715fc9563251 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
def right_rotate(arr, n, out_of_place, cur):
temp = arr[cur]
for i in range(cur, out_of_place, -1):
arr[i] = arr[i - 1]
arr[out_of_place] = temp
return arr
def re_arrange(arr, n):
out_of_place = -1
for index in range(n):
if (out_of_place >= 0):
if ((arr[index] >= 0 and arr[out_of_place] < 0) or
(arr[index] < 0 and arr[out_of_place] >= 0)):
arr = right_rotate(arr, n, out_of_place, index)
if (index-out_of_place > 2):
out_of_place += 2
else:
out_of_place = - 1
if (out_of_place == -1):
if ((arr[index] >= 0 and index % 2 == 0) or
(arr[index] < 0 and index % 2 == 1)):
out_of_place = index
return arr
|