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.

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;
}
Category >> Algorithms >> Interview  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Avinav Sharan on 2015-12-27

    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?

  2. Larry Okeke on 2016-3-25

    Recursion and binary search are common in programming. Though this author is good.

  3. Larry Okeke on 2016-3-25

    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. Arun on 2016-4-26

    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;”

  5. Ambi on 2016-5-7

    It doesn’t work for input array [1 5 1 1 1] with target as 5

  6. Soumya on 2016-10-6

    “You may assume no duplicate exists in the array.”

  7. shingooo on 2016-11-20

    Using another one is to avoid overflow.

Leave a comment

*