Challenge: Search in a Rotated Array
Solve the challenge of searching in a sorted and rotated array.
We'll cover the following
Problem statement
Given a sorted array of n
integers that has been rotated an unknown number of times, write some code to find an element in the array. Assume that the array was originally sorted in ascending order.
Input
A sorted array that has been rotated a number of times.
Output
The index of the element.
Sample input
arr = {7, 8, 9, 0, 3, 5, 6};
n = size of the array
key = 3; // Element that is being searched for
Sample output
result = 4; // Index of the element that was searched for
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.