🔄

Index of X in Rotated Sorted Array

Intermediate

Given a rotated sorted array and a target, find the index of the target using binary search.

15-20 min
Level 1 - Easy

Index of X in Rotated Sorted Array

Enter the index of the target in the rotated sorted array. If not found, enter -1.

Array:

[4,5,6,7,0,1,2]

Target: 0

Enter Index:

Index of X in Rotated Sorted Array - Pseudo Code

📋 Algorithm Pseudo Code


function searchInRotatedSortedArray(arr, target):
    n = length(arr)
    minIndex = findMinIndex(arr)
    // Search in left sorted part
    index = binarySearch(arr, 0, minIndex - 1, target)
    if index != -1:
        return index
    // Search in right sorted part
    index = binarySearch(arr, minIndex, n - 1, target)
    return index

function findMinIndex(arr):
    left = 0
    right = length(arr) - 1
    while left < right:
        mid = (left + right) // 2
        if arr[mid] < arr[right]:
            right = mid
        else:
            left = mid + 1
    return left

function binarySearch(arr, start, end, target):
    left = start
    right = end
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        else if target < arr[mid]:
            right = mid - 1
        else:
            left = mid + 1
    return -1
Progress0 / 4
Algorithm:Index of X in Rotated Sorted Array
🎯 How to Play

1. Study the array and target shown on the left

2. Enter the index of the target

3. Click "Check Solution" to verify

4. Use Tab to move between fields

5. Click "Next Level" to try more

🔄 Algorithm Steps

Step 1: Find the index of the minimum element (rotation point)

Step 2: Apply binary search in both sorted portions

Step 3: If not found, return -1

📊 Level Info

Difficulty: Easy

Array Size: 7

Target: 0