Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solution of Search in Rotated Sorted Array #1331

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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:[email protected]">E-Mail</a> |
| Tharindu Sooriyaarchchi | <a href="https://github.com/TharinduDilshan>Tharindu Sooriyaarchchi</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Samriddh Prasad | <a href="https://github.com/Samriddh2703">Samriddh Prasad</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Sovan Roy | <a href="https://github.com/SovanRoy10">Sovan Roy</a> | <a href="mailto:[email protected]">E-Mail</a>
| Edgar Gonzalez | <a href="https://github.com/Edgarzerocool">Edgar Gonzalez</a> | <a href="mailto:[email protected]">E-Mail</a> |


Expand Down