diff --git a/Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp b/Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp similarity index 70% rename from Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp rename to Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp index de180cc1..44eec40c 100644 --- a/Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp +++ b/Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp @@ -3,14 +3,13 @@ using namespace std; class Solution { public: - // Function to perform the search in a rotated array + int search(const vector& nums, int target) { - // Check if the array is empty + if (nums.empty()) { return -1; } - // Binary search to find the pivot point int left = 0, right = nums.size() - 1; while (left < right) { int mid = (left + right) / 2; @@ -22,14 +21,11 @@ class Solution { } } - // 'left' now contains the pivot index int pivot = left; - // Reset pointers for the second binary search left = 0; right = nums.size() - 1; - // Binary search to find the target in the rotated array while (left <= right) { int mid = (left + right) / 2; int midVal = nums[(mid + pivot) % nums.size()]; @@ -43,7 +39,6 @@ class Solution { } } - // If Target not found return -1; } };