From 791d9d2f93cbad7c7e58e7d6f1d0c627ad3e72d2 Mon Sep 17 00:00:00 2001 From: Avani Mathur <151984867+avanimathur@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:14:43 +0530 Subject: [PATCH 1/3] Add files via upload Day18 q2 solution --- .../solution_C++.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp 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/solution_C++.cpp new file mode 100644 index 00000000..de180cc1 --- /dev/null +++ b/Day-18/Day-18/q2: Search in Rotated Sorted Array/solution_C++.cpp @@ -0,0 +1,49 @@ +#include +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; + + if (nums[mid] > nums[right]) { + left = mid + 1; + } else { + right = mid; + } + } + + // '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()]; + + if (midVal == target) { + return (mid + pivot) % nums.size(); + } else if (midVal < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + // If Target not found + return -1; + } +}; 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 2/3] 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; } }; From fb0312ecffbe811f24bc114bd90c682fd5ee79b6 Mon Sep 17 00:00:00 2001 From: Bhumika Gupta <95757762+bh-g@users.noreply.github.com> Date: Tue, 23 Jan 2024 02:51:33 +0530 Subject: [PATCH 3/3] Update and rename Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp to Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp --- .../q2: Search in Rotated Sorted Array/avanimathur--C.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Day-18/{Day-18 => }/q2: Search in Rotated Sorted Array/avanimathur--C.cpp (100%) diff --git a/Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp b/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp similarity index 100% rename from Day-18/Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp rename to Day-18/q2: Search in Rotated Sorted Array/avanimathur--C.cpp