Skip to content

Commit c1d00c3

Browse files
Made changes according to PA
1 parent bf39427 commit c1d00c3

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

dsa-solutions/lc-solutions/0000-0099/0033-search-in-rotated-sorted-array.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ id: search-in-rotated-sorted-array
33
title: Search in Rotated Sorted Array (LeetCode)
44
difficulty: Medium
55
sidebar_label: 0033-SearchInRotatedSortedArray
6-
topics:
7-
- Array
8-
- Binary Search
9-
companies: []
106
description: Search for a target element in a rotated sorted array with distinct values using an algorithm with O(log n) runtime complexity.
117
---
128

@@ -19,45 +15,45 @@ description: Search for a target element in a rotated sorted array with distinct
1915

2016
## Problem Description
2117

22-
There is an integer array `nums` sorted in ascending order (with distinct values).
18+
There is an integer array $nums$ sorted in ascending order (with distinct values).
2319

24-
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).
20+
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).
2521

26-
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`.
22+
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$.
2723

28-
You must write an algorithm with O(log n) runtime complexity.
24+
You must write an algorithm with $O(log n)$ runtime complexity.
2925

3026
### Examples
3127

3228
#### Example 1
3329

34-
- **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0`
35-
- **Output:** `4`
36-
- **Explanation:** 0 is located at index 4 in the rotated sorted array `[4,5,6,7,0,1,2]`.
30+
- **Input:** $nums = [4,5,6,7,0,1,2]$, $target = 0$
31+
- **Output:** $4$
32+
- **Explanation:** 0 is located at index 4 in the rotated sorted array $[4,5,6,7,0,1,2]$.
3733

3834
#### Example 2
3935

40-
- **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3`
41-
- **Output:** `-1`
42-
- **Explanation:** 3 is not in `nums`, so return -1.
36+
- **Input:** $nums = [4,5,6,7,0,1,2]$, $target = 3$
37+
- **Output:** $-1$
38+
- **Explanation:** 3 is not in $nums$, so return -1.
4339

4440
#### Example 3
4541

46-
- **Input:** `nums = [1]`, `target = 0`
47-
- **Output:** `-1`
48-
- **Explanation:** 0 is not in `nums`, so return -1.
42+
- **Input:** $nums = [1]$, $target = 0$
43+
- **Output:** $-1$
44+
- **Explanation:** 0 is not in $nums$, so return -1.
4945

5046
### Constraints
5147

52-
- `1 <= nums.length <= 5000`
53-
- `-10^4 <= nums[i] <= 10^4`
54-
- All values of `nums` are unique.
55-
- `nums` is an ascending array that is possibly rotated.
56-
- `-10^4 <= target <= 10^4`
48+
- $v1 <= nums.length <= 5000$
49+
- $-10^4 <= nums[i] <= 10^4$
50+
- All values of $nums$ are unique.
51+
- $nums$ is an ascending array that is possibly rotated.
52+
- $-10^4 <= target <= 10^4$
5753

5854
### Approach
5955

60-
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.
56+
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.
6157

6258
1. **Find the Pivot Point:**
6359
- 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
7066

7167
#### Python
7268

73-
```
69+
```py
7470
class Solution:
7571
def search(self, nums: List[int], target: int) -> int:
7672
left, right = 0, len(nums) - 1
@@ -93,7 +89,7 @@ class Solution:
9389

9490
#### Java
9591

96-
```
92+
```java
9793
class Solution {
9894
public int search(int[] nums, int target) {
9995
int left = 0, right = nums.length - 1;
@@ -120,7 +116,7 @@ class Solution {
120116

121117
#### C++
122118

123-
```
119+
```cpp
124120
class Solution {
125121
public:
126122
int search(vector<int>& nums, int target) {
@@ -148,4 +144,4 @@ public:
148144
149145
### Conclusion
150146
151-
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.
147+
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.

0 commit comments

Comments
 (0)