0
0
DSA Javascriptprogramming~10 mins

Floor and Ceil in Sorted Array in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Floor and Ceil in Sorted Array
Start with sorted array and target
Initialize low=0, high=array.length-1
While low <= high
Calculate mid = Math.floor((low+high)/2)
Compare array[mid
low=mid+1
Loop until low > high
Return floor and ceil based on final low and high
We use binary search to find floor and ceil by narrowing search space until low passes high.
Execution Sample
DSA Javascript
function floorAndCeil(arr, target) {
  let low = 0, high = arr.length - 1;
  let floor = -1, ceil = -1;
  while (low <= high) {
    let mid = Math.floor((low + high) / 2);
    if (arr[mid] === target) return { floor: arr[mid], ceil: arr[mid] };
    else if (arr[mid] < target) {
      floor = arr[mid];
      low = mid + 1;
    } else {
      ceil = arr[mid];
      high = mid - 1;
    }
  }
  return { floor, ceil };
}
This code finds floor and ceil of target in sorted array using binary search.
Execution Table
StepOperationlowhighmidarr[mid]ComparisonfloorceilVisual State
1Initialize06----1-1[1, 2, 4, 6, 8, 10, 12]
2Calculate mid06366 < 7? Yes6-1[1, 2, 4, 6, 8, 10, 12]
3Update low46---6-1[1, 2, 4, 6, 8, 10, 12]
4Calculate mid4651010 > 7? Yes610[1, 2, 4, 6, 8, 10, 12]
5Update high44---610[1, 2, 4, 6, 8, 10, 12]
6Calculate mid44488 > 7? Yes68[1, 2, 4, 6, 8, 10, 12]
7Update high43---68[1, 2, 4, 6, 8, 10, 12]
8Exit loop43--low > high68[1, 2, 4, 6, 8, 10, 12]
💡 Loop ends because low (4) > high (3), floor is 6, ceil is 8
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 7Final
low044444
high666433
mid-354--
floor-166666
ceil-1-110888
Key Moments - 3 Insights
Why do we update floor when arr[mid] < target?
Because arr[mid] is less than target and could be the closest smaller value. See step 2 and 3 where floor updates to 6.
Why does the loop stop when low > high?
Because the search space is empty, meaning we have found the closest floor and ceil. See step 8 where low=4 and high=3.
What if target equals arr[mid]?
We return floor and ceil as that exact value immediately. This is shown in the code line inside the loop checking arr[mid] === target.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of floor after step 4?
A6
B10
C-1
D8
💡 Hint
Check the 'floor' column in row for step 4 in execution_table.
At which step does the condition low > high become true?
AStep 6
BStep 7
CStep 8
DStep 5
💡 Hint
Look at the 'Comparison' column and 'Exit loop' row in execution_table.
If target was 10, what would be the floor and ceil returned?
Afloor=8, ceil=10
Bfloor=10, ceil=10
Cfloor=10, ceil=12
Dfloor=6, ceil=8
💡 Hint
Recall the code returns exact match floor and ceil immediately if arr[mid] === target.
Concept Snapshot
Floor and Ceil in Sorted Array:
- Use binary search with low=0, high=length-1
- Calculate mid = Math.floor((low+high)/2)
- If arr[mid] == target, floor=ceil=arr[mid]
- If arr[mid] < target, floor=arr[mid], low=mid+1
- If arr[mid] > target, ceil=arr[mid], high=mid-1
- Loop until low > high
- Return floor and ceil found
Full Transcript
This concept finds the floor and ceil of a target number in a sorted array using binary search. We start with low and high pointers at the array ends. We calculate mid and compare arr[mid] with target. If equal, we return that value as both floor and ceil. If arr[mid] is less than target, we update floor and move low up. If arr[mid] is greater, we update ceil and move high down. We repeat until low passes high, then return the last floor and ceil values found. The execution table shows step-by-step how low, high, mid, floor, and ceil change. Key moments clarify why floor updates when arr[mid] < target, why the loop stops when low > high, and what happens on exact match. The visual quiz tests understanding of these steps and outcomes.