33. Search in Rotated Sorted Array LeetCode

 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



Approach 1 :

class Solution {
    public int search(int[] nums, int target) {
       
        int n=nums.length;

        for(int i=0;i<n;i++)
        {
            if(nums[i]==target)
            {
                return i;
            }
        }
        return -1;
    }
}


Approach 2:


class Solution {
    public int search(int[] nums, int target) {
        int pivot = findPivot(nums);
       
        // If the array is not rotated
        if (pivot == 0 || target < nums[0]) {
            return binarySearch(nums, pivot, nums.length - 1, target);
        } else {
            return binarySearch(nums, 0, pivot - 1, target);
        }
    }

    // Find index of the smallest element (pivot)
    static int findPivot(int[] nums) {
        int left = 0;
        int right = nums.length - 1;

        while (left < right) {
            int mid = left + (right - left) / 2;

            if (nums[mid] > nums[right]) {
                left = mid + 1; // Pivot is in the right half
            } else {
                right = mid; // Pivot is in the left half
            }
        }

        return left; // Pivot index (smallest element)
    }

    // Normal binary search between left and right
    static int binarySearch(int[] nums, int left, int right, int target) {
        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return -1; // Not found
    }
}

Approach 3:-

public class Solution {
    public int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        // Binary search with logic to handle rotated sorted array
        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (nums[mid] == target) {
                return mid; // Target found
            }

            // If the left half is sorted
            if (nums[mid] >= nums[left]) {
                // Check if the target lies within the sorted left half
                if (nums[left] <= target && target <= nums[mid]) {
                    right = mid - 1; // Move left
                } else {
                    left = mid + 1; // Move right
                }
            }
            // Else, the right half must be sorted
            else {
                // Check if the target lies within the sorted right half
                if (nums[mid] <= target && target <= nums[right]) {
                    left = mid + 1; // Move right
                } else {
                    right = mid - 1; // Move left
                }
            }
        }

        return -1; // Target not found
    }
}

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence