Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions C++/SearchInRotatedSortedArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// leetcode question number : 33
//question level : medium

/*
Github username: SovanRoy10
Aim: My aim is to give the correct solution of search in rotated sorted array (leetcode question no 33)
Date: 19 October, 2023

*/

// solution :
class Solution
{
public:
int pivot(vector<int> &nums)
{
int s = 0, l = nums.size() - 1;
while (s < l)
{
int mid = s + (l - s) / 2;
if (nums[mid] < nums[0])
l = mid;
else
s = mid + 1;
}
return l;
}
int binary_search(vector<int> &nums, int s, int l, int target)
{
while (s <= l)
{
int mid = s + (l - s) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
s = mid + 1;
else
l = mid - 1;
}
return -1;
}
int search(vector<int> &nums, int target)
{
int pivot_index = pivot(nums);
int ans = binary_search(nums, 0, pivot_index - 1, target);
if (ans != -1)
return ans;
return binary_search(nums, pivot_index, nums.size() - 1, target);
}
};

// video solution : https://www.youtube.com/watch?v=iXLMMbdjeNM
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
| Sneha Chaudhary | <a href="https://github.com/Edgarzerocool">Sneha Chaudhary</a> | <a href="mailto:chaudharysneha1610@gmail.com">E-Mail</a> |
| Tharindu Sooriyaarchchi | <a href="https://github.com/TharinduDilshan>Tharindu Sooriyaarchchi</a> | <a href="mailto:tdilshan2010@gmail.com">E-Mail</a> |
| Samriddh Prasad | <a href="https://github.com/Samriddh2703">Samriddh Prasad</a> | <a href="mailto:samriddh2703@gmail.com">E-Mail</a> |
| Sovan Roy | <a href="https://github.com/SovanRoy10">Sovan Roy</a> | <a href="mailto:roysovan00@gmail.com">E-Mail</a>
| Edgar Gonzalez | <a href="https://github.com/Edgarzerocool">Edgar Gonzalez</a> | <a href="mailto:edgar_gonzalezja@hotmail.com">E-Mail</a> |


Expand Down