Skip to content

Commit 707f3a6

Browse files
authored
Merge pull request #1680 from Abhinavcode13/patch-39
Create Rotatedarr.cpp
2 parents 0b75e02 + f5d0e5e commit 707f3a6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Arrays/Rotatedarr.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int search(std::vector<int>& nums, int target) {
4+
int low = 0, high = nums.size() - 1;
5+
6+
while (low <= high) {
7+
int mid = (low + high) / 2;
8+
9+
if (nums[mid] == target) {
10+
return mid;
11+
}
12+
13+
if (nums[low] <= nums[mid]) {
14+
if (nums[low] <= target && target < nums[mid]) {
15+
high = mid - 1;
16+
} else {
17+
low = mid + 1;
18+
}
19+
} else {
20+
if (nums[mid] < target && target <= nums[high]) {
21+
low = mid + 1;
22+
} else {
23+
high = mid - 1;
24+
}
25+
}
26+
}
27+
28+
return -1;
29+
}
30+
};

0 commit comments

Comments
 (0)