33. Search in Rotated Sorted Array(Solution || Leetcode Medium || Java)

Palakkgoyal
3 min readOct 31, 2022

--

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 (1 <= 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 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.

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 an ascending array that is possibly rotated.
  • -104 <= target <= 104

SOLUTION:

class Solution {
public int search(int[] nums, int target) {
//first check if the array is rotataed or not,if not apply simple binary search
if(nums[0] <= nums[nums.length — 1]){
return binarySearch(nums,target,0,nums.length — 1);
}

//Now, as the array is rotated we will first find the pivot index
int start = 0;
int end = nums.length — 1;
int pivot = Integer.MIN_VALUE;

while(start <= end){//applying binary search for finding pivot index
int mid = start + (end — start)/2;

if(nums[mid] > nums[mid + 1]){//this can happen only at the pivot
pivot = mid;
break;
}
else if(nums[mid] < nums[mid — 1]){//this also can happen only at pivot
pivot = mid — 1;
break;
}
else if(nums[mid] > nums[start]){//as all elements after pivot are smaller than the first element, so if any element is greater than the first element than it means that that element is between 0 and pivot index
start = mid + 1;
}else{
end = mid — 1;
}
}

//after finding pivot we will apply binary search for finding our target value
if(target >= nums[0]){
return binarySearch(nums,target,0,pivot);
}

return binarySearch(nums,target,pivot+1,nums.length-1);
}

public int binarySearch(int[] nums, int target, int start, int end){
//normal binary search for searchc in an array
while(start <= end){
int mid = start + (end — start)/2;

if(nums[mid] == target){
return mid;
}else if(nums[mid] < target){
start = mid + 1;
}else{
end = mid — 1;
}
}

return -1;
}
}

TIME COMPLEXITY:- O(log(n))

SPACE COMPLEXITY:-O(1)

ADVICE : Don’t lose heart if you didn’t get this at once, it also took me a dedicated whole day to grasp the logic. Have patience and trust on yourself.

Thank you for reading. If you have any queries, then please let me know in the comment section, I will surely response toward that.

--

--

Palakkgoyal
Palakkgoyal

Written by Palakkgoyal

Solutions to all your coding related problems at one point. DSA question on daily basis and much more.

No responses yet