Problem Challenge 2

Search in Rotated Array (medium) #

Given an array of numbers which is sorted in ascending order and also rotated by some arbitrary number, find if a given ‘key’ is present in it.

Write a function to return the index of the ‘key’ in the rotated array. If the ‘key’ is not present, return -1. You can assume that the given array does not have any duplicates.

Example 1:

Input: [10, 15, 1, 3, 8], key = 15
Output: 1
Explanation: '15' is present in the array at index '1'.
Created with Fabric.js 1.6.0-rc.1 1 3 8 10 15 Original array: Array after 2 rotations: 10 15 1 3 8

Example 2:

Input: [4, 5, 7, 9, 10, -1, 2], key = 10
Output: 4
Explanation: '10' is present in the array at index '4'.
Created with Fabric.js 1.6.0-rc.1 Original array: -1 2 4 5 7 9 10 4 5 7 9 10 -1 2 Array after 5 rotations:

Try it yourself #

Try solving this question here:

Output

0.703s

-1 -1

Mark as Completed
←    Back
Solution Review: Problem Challenge 1
Next    →
Solution Review: Problem Challenge 2