From 6e3e8697eb805fa11f738322be10ccd2c6b78310 Mon Sep 17 00:00:00 2001 From: Avani Mathur <151984867+avanimathur@users.noreply.github.com> Date: Sun, 21 Jan 2024 08:14:59 +0530 Subject: [PATCH] Update and rename solution_C++.cpp to avanimathur--C.cpp --- .../{solution_C++.cpp => avanimathur--C.cpp} | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) rename Day-18/Day-18/q2: Search in Rotated Sorted Array/{solution_C++.cpp => avanimathur--C.cpp} (70%) 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; } };