-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15 November
44 lines (29 loc) · 883 Bytes
/
15 November
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
q1 Search in Rotated sorted array
//IN THIS QUESTION, WE APPLIED THE SIMPLE LOGIC OF BINARY SEARCH IN AN ARRAY. DIRECT APPLICATION.
class Solution {
public:
int search(vector<int>& nums, int target) {
int st = 0, end= nums.size() -1;
while(st <= end){
int mid= st + (end-st)/2;
if(nums[mid] == target){
return mid;
}
if(nums[st] <= nums[mid]) {
if(nums[st]<= target && target <= nums[mid]){
end = mid -1;
} else {
st = mid +1;
}
} else{
if(nums[mid] <= target && target <= nums[end]){
st = mid+1;
} else {
end = mid -1;
}
}
}
return -1 ;
}
};
Q2