Back to questionsDSA Visualizers: 388 Coding Questions Search in Rotated Sorted Array II visualizer Learn Search in Rotated Sorted Array II with a step-by-step Binary Search visualizer, JavaScript source code and a worked example. Practice sorted arrays.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 2 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input {
"a": [
2,
5,
6,
0,
0,
1,
2
],
"k": 0,
"dupes": true
} Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = ({
2 a,k,dupes
3 },s)=>{
4 let l=0 ,r=a.length-1 ;
5 while (l<=r){
6 let m=(l+r)>>1 ;
7 s("Identify sorted half",{
8 Array:a,Bounds:[l,m,r]
9 },[l,m,r]);
10 if (a[m]===k)return dupes?true :m;
11 if (a[l]===a[m]&&a[m]===a[r]){
12 l++;
13 r--
14 }else if (a[l]<=a[m]){
15 if (a[l]<=k&&k<a[m])r=m-1 ;
16 else l=m+1
17 }else {
18 if (a[m]<k&&k<=a[r])l=m+1 ;
19 else r=m-1
20 }
21 }
22 return dupes?false :-1
23 };
Execution canvas Active ChangedSTEP 1 Identify sorted half Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
Worked example and algorithm walkthrough Topic: Binary Search · Sorted arrays
Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.
Example input {
"a": [
2,
5,
6,
0,
0,
1,
2
],
"k": 0,
"dupes": true
} Execution steps Identify sorted half Computed output true JavaScript reference implementation const solve = ({
a,k,dupes
},s)=>{
let l=0,r=a.length-1;
while(l<=r){
let m=(l+r)>>1;
s("Identify sorted half",{
Array:a,Bounds:[l,m,r]
},[l,m,r]);
if(a[m]===k)return dupes?true:m;
if(a[l]===a[m]&&a[m]===a[r]){
l++;
r--
}else if(a[l]<=a[m]){
if(a[l]<=k&&k<a[m])r=m-1;
else l=m+1
}else{
if(a[m]<k&&k<=a[r])l=m+1;
else r=m-1
}
}
return dupes?false:-1
};Continue learning Practice related DSA questions in the same topic.