Search in Rotated Array

Jesus PF
2 min readApr 25, 2021

The next medium level leetcode exercise provides a good understanding of search algorithms and array operations. Exercise: https://leetcode.com/problems/search-in-rotated-sorted-array/

Problem #33. Search in Rotated Sorted Array

There is an integer array nums sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is 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). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].

Given the array nums after the rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

Example 3:

Input: nums = [1], target = 0
Output: -1

Constraints:

  • 1 <= nums.length <= 5000
  • -104 <= nums[i] <= 104
  • All values of nums are unique.
  • nums is guaranteed to be rotated at some pivot.
  • -104 <= target <= 104

Follow up: Can you achieve this in O(log n) time complexity?

Result:

SuccessRuntime: 0 ms, faster than 100.00% of Java online submissions for Search in Rotated Sorted Array.Memory Usage: 38 MB, less than 88.11% of Java online submissions for Search in Rotated Sorted Array.

Solution:

The following solution was extracted from the discussion section and I will explain it here:

  • Find the index of the smallest number on the list using an binary search like loop that takes O(N) runtime
  • once the smallest number, or first element on the sorted list is found, we do a secondary binary search from i ->0 + rotationOffset to list lenght + rotation offset
class Solution {
public int search(int[] nums, int target) {
return search(nums,nums.length,target);
}


int search(int A[], int n, int target) {
int lo=0,hi=n-1;
// find the index of the smallest value using binary search.
// Loop will terminate since mid < hi, and lo or hi will shrink by at least 1.
// Proof by contradiction that mid < hi: if mid==hi, then lo==hi and loop would have been terminated.
while(lo<hi){
int mid=(lo+hi)/2;
if(A[mid]>A[hi]) lo=mid+1;
else hi=mid;
}
// lo==hi is the index of the smallest value and also the number of places rotated.
int rot=lo;
lo=0;hi=n-1;
// The usual binary search and accounting for rotation.
while(lo<=hi){
int mid=(lo+hi)/2;
int realmid=(mid+rot)%n;
if(A[realmid]==target)return realmid;
if(A[realmid]<target)lo=mid+1;
else hi=mid-1;
}
return -1;
}
}

--

--

Jesus PF

I am en electronics engineer, graduated from ITESM. Experienced working as functional validation and software development.