Skip to content

Commit 9d434f4

Browse files
authored
Added python Cdoe for search in sorted and rotated array and added de... (#12)
* Added python Cdoe for search in sorted and rotated array and added details to contribution * Added solution for search in a sorted and rotated array
1 parent bfb8b9d commit 9d434f4

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Time Complexity: O(logn)
3+
Space Complexity: O(1)
4+
5+
Point to observe: Each time either the left half will be sorted or the right half.
6+
Hence Binary Search is a good option.
7+
"""
8+
9+
class Solution:
10+
def search(self, nums: List[int], target: int) -> int:
11+
n = len(nums)
12+
low = 0
13+
high = n-1
14+
while low<=high:
15+
mid = (low+high)//2
16+
if nums[mid]==target:
17+
return mid
18+
19+
# if left half is sorted
20+
if nums[low]<=nums[mid]:
21+
if nums[low]<=target<nums[mid]:
22+
high = mid-1
23+
else:
24+
low = mid+1
25+
26+
# if right half is sorted
27+
else:
28+
if nums[mid]<target<=nums[high]:
29+
low = mid+1
30+
else:
31+
high = mid-1
32+
return -1
33+
34+
35+

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
152152
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
153153
| --- | --------------------------------------------------------------------- | -------------------------------------------------------------------- | --------- | --------- | ---------- | --- | ---------------------- |
154154
| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Java](./Java/May-LeetCoding-Challenge/Day-1-First-Bad-Version.java) <br> [JavaScript](./JavaScript/First-Bad-Version.js) | _O(logn)_ | _O(1)_ | Easy | | Binary Search |
155+
| 33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Python](./Python/search-in-rotated-sorted-array.py) | _O(logn)_ | _O(1)_ | Medium | | Binary Search |
155156

156157
<br/>
157158
<div align="right">
@@ -185,7 +186,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
185186
| [Lokendra Bohra](https://github.com/lokendra1704/) <br> <img src="https://github.com/lokendra1704.png" width="100" height="100"> | India | Python | [Leetcode](https://t.co/u0OByxhcHA) <br> [Hackerrank](https://www.hackerrank.com/lokendra17) |
186187
| [Yuri Spiridonov](https://github.com/YuriSpiridonov) <br> <img src="https://github.com/YuriSpiridonov.png" width="100" height="100"> | Russia | Python | [Twitter](https://twitter.com/YuriSpiridonov)<br>[Leetcode](https://leetcode.com/yurispiridonov/)<br>[Hackerrank](https://www.hackerrank.com/YuriSpiridonov) |
187188
| [Naveen Kashyap](https://github.com/naveenkash) <br> <img src="https://github.com/naveenkash.png" width="100" height="100"> | India | Javascript | [Twitter](https://twitter.com/naveen_kashyapp)<br>[Leetcode](https://leetcode.com/naveenkash/) |
188-
189+
| [Sachin Singh Negi](https://github.com/sachinnegi) <br> <img src="https://github.com/sachinnegi.png" width="100" height="100"> | India | Python | [Twitter](https://twitter.com/SachinSinghNe17)<br>[Leetcode](https://leetcode.com/negisachin688/)<br>[Hackerrrak](https://www.hackerrank.com/negisachin688) | |
189190
<br/>
190191
<div align="right">
191192
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)