You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Search for a target element in a rotated sorted array with distinct values using an algorithm with O(log n) runtime complexity.
11
7
---
12
8
@@ -19,45 +15,45 @@ description: Search for a target element in a rotated sorted array with distinct
19
15
20
16
## Problem Description
21
17
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).
23
19
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).
25
21
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$.
27
23
28
-
You must write an algorithm with O(log n) runtime complexity.
24
+
You must write an algorithm with $O(log n)$ runtime complexity.
29
25
30
26
### Examples
31
27
32
28
#### Example 1
33
29
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]$.
37
33
38
34
#### Example 2
39
35
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.
43
39
44
40
#### Example 3
45
41
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.
49
45
50
46
### Constraints
51
47
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$
57
53
58
54
### Approach
59
55
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.
61
57
62
58
1.**Find the Pivot Point:**
63
59
- 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
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