-
Notifications
You must be signed in to change notification settings - Fork 10
/
search_from_rotated_array.py
229 lines (212 loc) · 1.9 KB
/
search_from_rotated_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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""Search in a
Sorted and Rotated
Array
Given
a
sorted and rotated
array
A
of
N
distinct
elements
which is rotated
at
some
point, and given
an
element
K.The
task is to
find
the
index
of
the
given
element
K in the
array
A.
Input:
The
first
line
of
the
input
contains
an
integer
T, denoting
the
total
number
of
test
cases.Then
T
test
cases
follow.Each
test
case
consists
of
three
lines.The
first
line
of
each
test
case
contains
an
integer
N
denoting
the
size
of
the
given
array.The
second
line
of
each
test
case
contains
N
space - separated
integers
denoting
the
elements
of
the
array
A.The
third
line
of
each
test
case
contains
an
integer
K
denoting
the
element
to
be
searched in the
array.
Output:
For
each
test
case, print
the
index(0
based
indexing) of
the
element
K in a
new
line,
if K does not exist in the array then print -1.
User
Task:
Complete
Search()
function and
return the
index
of
the
element
K if found in the
array.If
the
element is not present, then
return -1.
Expected
Time
Complexity: O(log
N).
Expected
Auxiliary
Space: O(1).
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 107
0 ≤ Ai ≤ 108
1 ≤ K ≤ 108
Example:
Input:
3
9
5
6
7
8
9
10
1
2
3
10
3
3
1
2
1
4
3
5
1
2
6
Output:
5
1
-1
Explanation:
Testcase
1: 10 is found
at
index
5.
Testcase
2: 1 is found
at
index
1.
Testcase
3: 6
doesn
't exist.
"""
def Search(arr,n,k):
#code here
if k in arr:
return arr.index(k)
else:
return -1
#{
# Driver Code Starts
#Initial Template for Python 3
if __name__ == '__main__':
tcs=int(input())
for _ in range(tcs):
n=int(input())
arr=[int(x) for x in input().split()]
k=int(input())
print(Search(arr,n,k))
# } Driver Code Ends