From 01a34c669f239354d5d20dc104488b64a0ed3bd3 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Wed, 5 Jun 2024 15:50:34 +0530 Subject: [PATCH 1/4] Added solution for search-in-rotated-sorted-array --- .../lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md b/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md new file mode 100644 index 000000000..e69de29bb From bf39427ce1388ae5871f9b9331e98088389018e4 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Wed, 5 Jun 2024 16:09:53 +0530 Subject: [PATCH 2/4] Added solution for search-in-rotated-sorted-array --- .../0033-search-in-rotated-sorted-array.md | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md b/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md index e69de29bb..9f91a9fcc 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md +++ b/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md @@ -0,0 +1,151 @@ +--- +id: search-in-rotated-sorted-array +title: Search in Rotated Sorted Array (LeetCode) +difficulty: Medium +sidebar_label: 0033-SearchInRotatedSortedArray +topics: + - Array + - Binary Search +companies: [] +description: Search for a target element in a rotated sorted array with distinct values using an algorithm with O(log n) runtime complexity. +--- + +## Problem Description + + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [Merge Two Sorted Lists](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/search-in-rotated-sorted-array/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | + +## Problem Description + +There is an integer array `nums` sorted in ascending order (with distinct values). + +Prior to being passed to your function, `nums` is possibly rotated at an unknown pivot index `k` (0 <= k < nums.length) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (0-indexed). + +Given the array `nums` after the possible rotation and an integer `target`, return the index of `target` if it is in `nums`, or -1 if it is not in `nums`. + +You must write an algorithm with O(log n) runtime complexity. + +### Examples + +#### Example 1 + +- **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0` +- **Output:** `4` +- **Explanation:** 0 is located at index 4 in the rotated sorted array `[4,5,6,7,0,1,2]`. + +#### Example 2 + +- **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3` +- **Output:** `-1` +- **Explanation:** 3 is not in `nums`, so return -1. + +#### Example 3 + +- **Input:** `nums = [1]`, `target = 0` +- **Output:** `-1` +- **Explanation:** 0 is not in `nums`, so return -1. + +### Constraints + +- `1 <= nums.length <= 5000` +- `-10^4 <= nums[i] <= 10^4` +- All values of `nums` are unique. +- `nums` is an ascending array that is possibly rotated. +- `-10^4 <= target <= 10^4` + +### Approach + +To search for a target element in a rotated sorted array with distinct values with O(log n) runtime complexity, we can use the binary search algorithm. + +1. **Find the Pivot Point:** + - Perform binary search to find the pivot element, which is the smallest element in the array. This element divides the array into two sorted subarrays. + +2. **Perform Binary Search:** + - Based on the pivot element, determine which half of the array the target might be in. + - Perform binary search in the appropriate half to find the target element. + +### Solution Code + +#### Python + +``` +class Solution: + def search(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target: + return mid + elif nums[left] <= nums[mid]: + if nums[left] <= target < nums[mid]: + right = mid - 1 + else: + left = mid + 1 + else: + if nums[mid] < target <= nums[right]: + left = mid + 1 + else: + right = mid - 1 + return -1 +``` + +#### Java + +``` +class Solution { + public int search(int[] nums, int target) { + int left = 0, right = nums.length - 1; + while (left <= right) { + int mid = (left + right) / 2; + if (nums[mid] == target) + return mid; + else if (nums[left] <= nums[mid]) { + if (nums[left] <= target && target < nums[mid]) + right = mid - 1; + else + left = mid + 1; + } else { + if (nums[mid] < target && target <= nums[right]) + left = mid + 1; + else + right = mid - 1; + } + } + return -1; + } +} +``` + +#### C++ + +``` +class Solution { +public: + int search(vector& nums, int target) { + int left = 0, right = nums.size() - 1; + while (left <= right) { + int mid = (left + right) / 2; + if (nums[mid] == target) + return mid; + else if (nums[left] <= nums[mid]) { + if (nums[left] <= target && target < nums[mid]) + right = mid - 1; + else + left = mid + 1; + } else { + if (nums[mid] < target && target <= nums[right]) + left = mid + 1; + else + right = mid - 1; + } + } + return -1; + } +}; +``` + +### Conclusion + +The above solution effectively searches for a target element in a rotated sorted array with distinct values using the binary search algorithm with O(log n) runtime complexity. It handles all edge cases and constraints specified in the problem statement. From c1d00c32082dd30770e68d1e39d46c534ab8e654 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Thu, 6 Jun 2024 06:11:40 +0530 Subject: [PATCH 3/4] Made changes according to PA --- .../0033-search-in-rotated-sorted-array.md | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md b/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md index 9f91a9fcc..526b3a224 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md +++ b/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md @@ -3,10 +3,6 @@ id: search-in-rotated-sorted-array title: Search in Rotated Sorted Array (LeetCode) difficulty: Medium sidebar_label: 0033-SearchInRotatedSortedArray -topics: - - Array - - Binary Search -companies: [] description: Search for a target element in a rotated sorted array with distinct values using an algorithm with O(log n) runtime complexity. --- @@ -19,45 +15,45 @@ description: Search for a target element in a rotated sorted array with distinct ## Problem Description -There is an integer array `nums` sorted in ascending order (with distinct values). +There is an integer array $nums$ sorted in ascending order (with distinct values). -Prior to being passed to your function, `nums` is possibly rotated at an unknown pivot index `k` (0 <= k < nums.length) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (0-indexed). +Prior to being passed to your function, $nums$ is possibly rotated at an unknown pivot index $k$ (0 <= k < nums.length) such that the resulting array is $[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]$ (0-indexed). -Given the array `nums` after the possible rotation and an integer `target`, return the index of `target` if it is in `nums`, or -1 if it is not in `nums`. +Given the array $nums$ after the possible rotation and an integer $target$, return the index of $target$ if it is in $nums, or -1 if it is not in $nums$. -You must write an algorithm with O(log n) runtime complexity. +You must write an algorithm with $O(log n)$ runtime complexity. ### Examples #### Example 1 -- **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0` -- **Output:** `4` -- **Explanation:** 0 is located at index 4 in the rotated sorted array `[4,5,6,7,0,1,2]`. +- **Input:** $nums = [4,5,6,7,0,1,2]$, $target = 0$ +- **Output:** $4$ +- **Explanation:** 0 is located at index 4 in the rotated sorted array $[4,5,6,7,0,1,2]$. #### Example 2 -- **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3` -- **Output:** `-1` -- **Explanation:** 3 is not in `nums`, so return -1. +- **Input:** $nums = [4,5,6,7,0,1,2]$, $target = 3$ +- **Output:** $-1$ +- **Explanation:** 3 is not in $nums$, so return -1. #### Example 3 -- **Input:** `nums = [1]`, `target = 0` -- **Output:** `-1` -- **Explanation:** 0 is not in `nums`, so return -1. +- **Input:** $nums = [1]$, $target = 0$ +- **Output:** $-1$ +- **Explanation:** 0 is not in $nums$, so return -1. ### Constraints -- `1 <= nums.length <= 5000` -- `-10^4 <= nums[i] <= 10^4` -- All values of `nums` are unique. -- `nums` is an ascending array that is possibly rotated. -- `-10^4 <= target <= 10^4` +- $v1 <= nums.length <= 5000$ +- $-10^4 <= nums[i] <= 10^4$ +- All values of $nums$ are unique. +- $nums$ is an ascending array that is possibly rotated. +- $-10^4 <= target <= 10^4$ ### Approach -To search for a target element in a rotated sorted array with distinct values with O(log n) runtime complexity, we can use the binary search algorithm. +To search for a target element in a rotated sorted array with distinct values with $O(log n)$ runtime complexity, we can use the binary search algorithm. 1. **Find the Pivot Point:** - Perform binary search to find the pivot element, which is the smallest element in the array. This element divides the array into two sorted subarrays. @@ -70,7 +66,7 @@ To search for a target element in a rotated sorted array with distinct values wi #### Python -``` +```py class Solution: def search(self, nums: List[int], target: int) -> int: left, right = 0, len(nums) - 1 @@ -93,7 +89,7 @@ class Solution: #### Java -``` +```java class Solution { public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; @@ -120,7 +116,7 @@ class Solution { #### C++ -``` +```cpp class Solution { public: int search(vector& nums, int target) { @@ -148,4 +144,4 @@ public: ### Conclusion -The above solution effectively searches for a target element in a rotated sorted array with distinct values using the binary search algorithm with O(log n) runtime complexity. It handles all edge cases and constraints specified in the problem statement. +The above solution effectively searches for a target element in a rotated sorted array with distinct values using the binary search algorithm with $O(log n)$ runtime complexity. It handles all edge cases and constraints specified in the problem statement. From da204983022c475d33ae375c7c930c28a0992111 Mon Sep 17 00:00:00 2001 From: Vijay Shanker Sharma Date: Thu, 6 Jun 2024 16:48:41 +0530 Subject: [PATCH 4/4] Made changes according to PA --- .../0033-search-in-rotated-sorted-array.md | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md b/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md index 526b3a224..66339a1b8 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md +++ b/dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md @@ -1,25 +1,23 @@ --- id: search-in-rotated-sorted-array title: Search in Rotated Sorted Array (LeetCode) -difficulty: Medium sidebar_label: 0033-SearchInRotatedSortedArray description: Search for a target element in a rotated sorted array with distinct values using an algorithm with O(log n) runtime complexity. --- ## Problem Description - | Problem Statement | Solution Link | LeetCode Profile | | :---------------- | :------------ | :--------------- | | [Merge Two Sorted Lists](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/search-in-rotated-sorted-array/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | ## Problem Description -There is an integer array $nums$ sorted in ascending order (with distinct values). +There is an integer array nums sorted in ascending order (with distinct values). -Prior to being passed to your function, $nums$ is possibly rotated at an unknown pivot index $k$ (0 <= k < nums.length) such that the resulting array is $[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]$ (0-indexed). +Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). -Given the array $nums$ after the possible rotation and an integer $target$, return the index of $target$ if it is in $nums, or -1 if it is not in $nums$. +Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. You must write an algorithm with $O(log n)$ runtime complexity. @@ -27,28 +25,28 @@ You must write an algorithm with $O(log n)$ runtime complexity. #### Example 1 -- **Input:** $nums = [4,5,6,7,0,1,2]$, $target = 0$ -- **Output:** $4$ -- **Explanation:** 0 is located at index 4 in the rotated sorted array $[4,5,6,7,0,1,2]$. +- **Input:** nums = [4,5,6,7,0,1,2], target = 0 +- **Output:** 4 +- **Explanation:** 0 is located at index 4 in the rotated sorted array [4,5,6,7,0,1,2]. #### Example 2 -- **Input:** $nums = [4,5,6,7,0,1,2]$, $target = 3$ -- **Output:** $-1$ -- **Explanation:** 3 is not in $nums$, so return -1. +- **Input:** nums = [4,5,6,7,0,1,2], target = 3 +- **Output:** -1 +- **Explanation:** 3 is not in nums, so return -1. #### Example 3 -- **Input:** $nums = [1]$, $target = 0$ -- **Output:** $-1$ -- **Explanation:** 0 is not in $nums$, so return -1. +- **Input:** nums = [1], target = 0 +- **Output:** -1 +- **Explanation:** 0 is not in nums, so return -1. ### Constraints - $v1 <= nums.length <= 5000$ - $-10^4 <= nums[i] <= 10^4$ - All values of $nums$ are unique. -- $nums$ is an ascending array that is possibly rotated. +- nums is an ascending array that is possibly rotated. - $-10^4 <= target <= 10^4$ ### Approach