Developer

This experience deserves a bigger screen

The DSA Visualizer is designed for deep focus,
side-by-side code, and real-time execution insight.

On small screens, that level of clarity can't be preserved.

Open on a tablet (landscape) or desktop for the intended experience.

Desktop experience preview

Search in Rotated Sorted ArrayStep-by-Step Visualization

This problem adds a twist to standard binary search by rotating the sorted array, requiring careful handling of the sorted halves.

Problem Statement

There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k. Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

Approach

Perform binary search. In each step, at least one half of the array (left to mid or mid to right) will be sorted. Check if the target lies within the sorted half range. If it does, eliminate the other half. If not, search the other half.

Time & Space Complexity

Time complexity is O(log n). Space complexity is O(1).

Interactive Visualization

Loading Interactive Visualizer...

Click "Play" or use arrow keys to step through the execution.