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.
Input: nums = [4,5,6,7,0,1,2], target = 0
Input: nums = [4,5,6,7,0,1,2], target = 3
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.
Other Posts You May Like
- Binary Search Algorithm
- Implement sqrt function using binary search
- Check perfect square using binary search
- Find a first unique character in a string
- Remove Duplicates from sorted array