ntree.aiSearch in Rotated Sorted Array
Solutions
1. Brute Force
O(n) time | O(1) space
Intuition
We can check every element one by one and return the index of the first one that is equal to the target.
Algorithm
- Iterate through the loop from the first element to the last element with
iindex. - Return
iif the element atiis equal to the target. - Return
-1when the loop finishes.
target = 0
0123456
i
4 ≠ 0
1 / 6
target = 0
0123456
i
Code
Code
def search(nums: list[int], target: int) -> int:
for i in range(len(nums)):
if nums[i] == target:
return i
return -1/**
* @param {number[]} nums
* @param {number} target
* @returns {number}
*/
var search = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === target) return i
}
return -1
}class Solution {
public int search(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) return i;
}
return -1;
}
}2. Binary Search
O(logn) time | O(1) space
Intuition
We can use the binary search technique starting from the middle element instead of checking every element.
As the array is rotated, one of the halves around the middle element is sorted.
We can tell if the target element is in this half only by looking at its two ends.
Algorithm
- Create
leftandrightinteger variables with the start and end indexes of the array as initial values. - Iterate through the loop while
leftis less than or equal toright. - Create
mid, the middle index betweenleftandright, and return it if the element there is equal to the target. - If the element at
leftis less than or equal to the element atmid, the left half is the sorted one:- Assign
mid - 1torightif the target is between the elements atleftandmid, dropping the right half. - Otherwise assign
mid + 1toleft, dropping the left half.
- Assign
- Otherwise the right half is the sorted one:
- Assign
mid + 1toleftif the target is between the elements atmidandright, dropping the left half. - Otherwise assign
mid - 1toright, dropping the right half.
- Assign
- Return
-1when the loop finishes.
target = 0
mid
0123456
left
right
7 ≠ 0, 0 is not between 4 and 7
1 / 4
target = 0
mid
0123456
left
right
Code
Code
def search(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
if nums[left] <= nums[mid]:
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
elif nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1/**
* @param {number[]} nums
* @param {number} target
* @returns {number}
*/
var search = function (nums, target) {
let left = 0
let right = nums.length - 1
while (left <= right) {
const mid = Math.floor((left + right) / 2)
if (nums[mid] === target) return mid
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
}class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target) return mid;
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;
}
}def search(nums: list[int], target: int) -> int: pass Press Visualise to step through your code.