-
Notifications
You must be signed in to change notification settings - Fork 618
/
inverseArray.py
60 lines (51 loc) · 1.12 KB
/
inverseArray.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
'''INVERSE OF AN ARRAY
Problem statement: If the array elements are swapped with their correseponding
indices,we get the inverse of an array
NOTE: The elements must be unique and less than the length of array.
'''
#inserts the elements in an array of size n
def inputElements(n):
arr=[]
for i in range(n):
#enter element to insert in array
element=int(input())
arr.append(element)
print("original array:",arr)
return arr
#function to find inverse of array
def inverseArray(lst):
inverted_array=[]
for i in range(0,len(lst)):
k=lst[i]
inverted_array.insert(k,i)
return inverted_array
#main code
# enter length of array
array_length=int(input())
arr=inputElements(array_length)
arr2=inverseArray(arr)
print("Inverse of array :",arr2)
'''
TestCases :
Input :
5
2
4
1
0
3
Output:
original array: [2,4,1,0,3]
Inverse of array: [3,2,0,4,1]
Input :
4
1
3
0
2
Output :
Original array:[1,3,0,2]
Inverse of array:[2,0,3,1]
Time Complexity: O(n) for traversing array of characters where size is n
Space Complexity:O(n),where n is size of array
'''