Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use the triple-reverse technique: first reverse the entire array, then reverse the first k elements, then reverse the remaining elements. Each reverse uses two converging pointers. Normalize k by taking k mod n to handle cases where k exceeds array length.
Reversing the whole array puts elements in the right relative order but backwards. The two subsequent partial reverses flip each segment back to the correct order, completing the rotation.
Updated Feb 2026