-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrearrange_array.py
44 lines (33 loc) · 1.11 KB
/
rearrange_array.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
def rearrange_array(arr):
"""
Rearrange a given array so that arr[i] becomes arr[arr[i]]
with O(1) extra space. The values in the array will become the new indizes.
Time complexity: O(n + n)
Space complexity: O(1)
"""
n = len(arr)
def is_encoded(num):
return num >= n
def encode(original_value, new_value):
return n + n * original_value + new_value
def decode(num):
return ((num / n) - 1, num % n)
def decode_new(num):
return decode(num)[1]
def decode_original(num):
if is_encoded(num):
return decode(num)[0]
else:
return num
for i in range(0, len(arr)):
new_value = decode_original(arr[arr[i]])
arr[i] = encode(arr[i], new_value)
for i in range(0, len(arr)):
arr[i] = decode_new(arr[i])
return arr
def test_rearrange_array():
assert rearrange_array([0, 1]) == [0, 1]
assert rearrange_array([1, 0]) == [0, 1]
assert rearrange_array([2, 0, 1, 4, 3, 6, 5]) == [1, 2, 0, 3, 4, 5, 6]
assert rearrange_array([0]) == [0]
assert rearrange_array([]) == []