LeetCode – Search in Rotated Sorted Array (Java)

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array.

Analysis

In order to use binary search on the rotated sorted array, we need to determine how to update the left and right pointers. There are two major cases as shown below:

Once the two cases are identified, the problem is straightforward to solve. We only need to check if the target element is in the sorted side, and based on that move left or right pointers.

Java Solution 1- Recusive

public int search(int[] nums, int target) {
    return binarySearch(nums, 0, nums.length-1, target);
}
 
public int binarySearch(int[] nums, int left, int right, int target){
    if(left>right) 
        return -1;
 
    int mid = left + (right-left)/2;
 
    if(target == nums[mid])
        return mid;
 
    if(nums[left] <= nums[mid]){
        if(nums[left]<=target && target<nums[mid]){
          return binarySearch(nums,left, mid-1, target);
        }else{
          return  binarySearch(nums, mid+1, right, target);
        }
    }else {
        if(nums[mid]<target&& target<=nums[right]){
          return  binarySearch(nums,mid+1, right, target);
        }else{
          return  binarySearch(nums, left, mid-1, target);
        }
    }
}

Java Solution 2 – Iterative

public int search(int[] nums, int target) {
    int left = 0;
    int right= nums.length-1;
 
    while(left<=right){
        int mid = left + (right-left)/2;
        if(target==nums[mid])
            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;
}

10 thoughts on “LeetCode – Search in Rotated Sorted Array (Java)”

  1. The solution might assume the array is in ascending order and gets rotated. Please clarify.

  2. I almost did the same thing except i did “mid = (startIndex + endIndex)/2;” and my code failed.
    Can you explain why you did “mid = left + (right-left)/2;”

  3. public class search_rotated_array{

    private static int[] arr;

    private static int find = 0;

    public static void main(String[] args){

    arr = new int[] {6, 7, 8, 9, 10, 1, 2, 3, 4, 5};

    find = 11;

    System.out.println(solution());

    }

    public static int solution(){

    int left = 0;

    int right = arr.length-1;

    int middle = (left+right)/2;

    return recurse(left, middle, right);

    }

    public static int recurse(int left, int middle, int right){

    log("Left " + arr[left] + " right " + arr[right]);

    if(arr[left]== find) return left;

    if(arr[right] == find) return right;

    if(right-left==1) return -1;

    int new_middle;

    if(arr[left] = find ){

    new_middle = (left+middle)/2;

    return recurse(left, new_middle, middle);

    }

    new_middle = (middle+1 + right) /2;

    return recurse(middle, new_middle, right);

    }

    }


  4. I am curious how you came up with this solution, did it just strike you, or you did you analogized from some other problem you had solved earlier, or just gave it too many hours?

Leave a Comment