0
0
DSA C++programming~10 mins

Floor and Ceil in Sorted Array in DSA C++ - 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=n-1
While low <= high
Calculate mid = (low+high)/2
Compare arr[mid
Equal
Floor=arr[mid
Repeat until low > high
Return Floor and Ceil values
This flow shows how binary search narrows down to find floor and ceil values for a target in a sorted array.
Execution Sample
DSA C++
int floor = -1, ceil = -1;
int low = 0, high = n - 1;
while (low <= high) {
  int mid = (low + high) / 2;
  if (arr[mid] == target) {
    floor = ceil = arr[mid];
    break;
  } else if (arr[mid] < target) {
    floor = arr[mid];
    low = mid + 1;
  } else {
    ceil = arr[mid];
    high = mid - 1;
  }
}
This code finds floor and ceil of target in a 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=6, ceil=8 found.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
low04444
high66433
mid-354-
floor-16666
ceil-1-11088
Key Moments - 3 Insights
Why do we update floor when arr[mid] < target but update ceil when arr[mid] > target?
Because floor is the greatest value less than or equal to target, so when arr[mid] < target, arr[mid] is a candidate floor. Similarly, ceil is the smallest value greater than or equal to target, so when arr[mid] > target, arr[mid] is a candidate ceil. This is shown in steps 2, 4, and 6 in the execution_table.
Why does the loop stop when low > high?
The loop stops when low passes high because all possible positions have been checked. At this point, floor and ceil hold the closest values to the target. This is shown at step 8 in the execution_table.
What if the target is exactly equal to arr[mid]?
If arr[mid] equals target, both floor and ceil are set to arr[mid], and the search can stop immediately. This case is handled in the code but not shown in this example since target=7 is not in the array.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of floor after step 4?
A10
B6
C-1
D8
💡 Hint
Check the 'Floor' column at step 4 in the execution_table.
At which step does the condition low > high become true, ending the loop?
AStep 6
BStep 7
CStep 8
DStep 5
💡 Hint
Look at the 'Exit loop' row and the 'low' and 'high' values in the execution_table.
If the target was 10, what would floor and ceil be after the loop?
Afloor=10, ceil=10
Bfloor=10, ceil=12
Cfloor=8, ceil=10
Dfloor=6, ceil=8
💡 Hint
When target equals an element in the array, floor and ceil are both that element.
Concept Snapshot
Floor and Ceil in Sorted Array:
- Use binary search with low=0, high=n-1
- Calculate mid = (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
- Result: floor and ceil closest to target
Full Transcript
This concept shows how to find floor and ceil values of a target 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, floor and ceil are arr[mid]. If arr[mid] is less, update floor and move low up. If arr[mid] is more, update ceil and move high down. Repeat until low passes high. The final floor and ceil are the closest values to the target. The execution table traces these steps with variable values and array state.