LeetCode - Search in Rotated Sorted Array - The Coding Shala
Home >> Interview Questions >> search in rotated sorted array LeetCode - Search in Rotated Sorted Array In this post, you will learn how to search an element in a rotated sorted array and returns its index. We will use the binary search algorithm here. Suppose an array sorted in ascending order 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. 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 Search in Rotated Sorted Array Java We can search in the array using the loop and return if the target is found or not but this would take O(n) time. this is not a good idea here. Instead of using normal search we will use Binary Search Algorithm. Java Program: class Solution { publ...