forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_insert_position.py
41 lines (34 loc) · 1.05 KB
/
search_insert_position.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
"""
Given a sorted array and a target value,
return the index if the target is found.
If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
"""
# taking the input from the user
x = list(map(int, input("Enter the elements in the list: ").strip().split()))
target = int(input("Enter the Element whose index has to be found: "))
# finding whether the target value is in the list or not
if target in x:
a = x.index(target)
print(
"The number is present at the index {}.".format(a)
) # if target is present then printing the index
else:
x.append(target) # adding the target in the list
x.sort() # sorting the list
a = x.index(target)
print(
"Number not found. It can be inserted at index {}.".format(a)
) # printing the target index
'''
Time complexity : O(N)
Space complexity : O(N)
'''
'''
Test Case :
Input:
Enter the elements in the list:[1,3,5,6]
Enter the Element whose index has to be found: 5
Output:
The number is present at the index {}. 2
'''