Skip to content

Commit

Permalink
Update and rename solution_C++.cpp to avanimathur--C.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
avanimathur authored Jan 21, 2024
1 parent 814ead8 commit 6e3e869
Showing 1 changed file with 2 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ using namespace std;

class Solution {
public:
// Function to perform the search in a rotated array

int search(const vector<int>& 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;
Expand All @@ -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()];
Expand All @@ -43,7 +39,6 @@ class Solution {
}
}

// If Target not found
return -1;
}
};

0 comments on commit 6e3e869

Please sign in to comment.