BackhardRecursion

Rotated Sequence Search Solution

Problem Statement

Given a rotated sorted sequence of distinct integers and a target value, find the index of the target in the sequence if it exists, otherwise return -1. The sequence was originally sorted in ascending order but has been rotated an unknown number of times.

Example 1
Input
[27, 31, 34, 41, 4, 17, 23], 31
Output
1

Explanation: The target value 31 is found at index 1 in the rotated sequence.

Example 2
Input
[5, 7, 9, 11, 2, 3], 9
Output
2

Explanation: The target value 9 is found at index 2 in the rotated sequence.

Example 3
Input
[13, 17, 19, 23, 29, 2, 5], 17
Output
1

Explanation: The target value 17 is found at index 1 in the rotated sequence.

Example 4
Input
[1, 3, 5, 7], 0
Output
-1

Explanation: The target value 0 is not found in the sequence.

Constraints

  • 1 <= sequence length <= 10^4
  • -10^4 <= target value <= 10^4
  • All elements in the sequence are distinct.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free