0
0
DSA Goprogramming~10 mins

Floor and Ceil in Sorted Array in DSA Go - 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
Update floor
Move low=mid+1
Repeat loop
Return floor and ceil values
We use binary search to find floor and ceil by narrowing the search range and updating floor or ceil pointers based on comparisons.
Execution Sample
DSA Go
arr := []int{1, 3, 5, 7, 9}
target := 6
floor, ceil := -1, -1
low, high := 0, len(arr)-1
for low <= high {
  mid := (low + high) / 2
  if arr[mid] == target {
    floor, ceil = arr[mid], 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 6 in a sorted array using binary search.
Execution Table
StepOperationmidarr[mid]ComparisonfloorceillowhighVisual State
1Initialize----1-104[1, 3, 5, 7, 9]
2Calculate mid255 < 65-134[1, 3, 5, 7, 9]
3Calculate mid377 > 65732[1, 3, 5, 7, 9]
4Exit loop--low(3) > high(2)5732[1, 3, 5, 7, 9]
💡 Loop ends because low (3) > high (2), floor is 5, ceil is 7
Variable Tracker
VariableStartAfter Step 2After Step 3Final
floor-1555
ceil-1-177
low0333
high4422
mid-23-
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 or equal value, so we save it as floor (see step 2 in execution_table).
Why do we update ceil when arr[mid] > target?
Because arr[mid] is greater than target and could be the closest greater or equal value, so we save it as ceil (see step 3 in execution_table).
Why does the loop stop when low > high?
Because the search space is empty, meaning we have found the closest floor and ceil values (see step 4 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of floor after step 2?
A5
B-1
C7
D3
💡 Hint
Check the 'floor' column in row for step 2 in execution_table.
At which step does the condition low <= high become false?
AStep 2
BStep 3
CStep 4
DNever
💡 Hint
Look at the 'low' and 'high' values in execution_table rows and see when low > high.
If the target was 7, what would floor and ceil be after the loop?
Afloor=5, ceil=7
Bfloor=7, ceil=7
Cfloor=7, ceil=9
Dfloor=3, ceil=5
💡 Hint
If arr[mid] == target, floor and ceil both equal target (see code in execution_sample).
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], move low=mid+1
- If arr[mid] > target, ceil=arr[mid], move high=mid-1
- Loop ends when low > high, return floor and ceil
Full Transcript
We find floor and ceil in a sorted array by binary searching for the target. We keep track of floor as the largest value less than or equal to target, and ceil as the smallest value greater than or equal to target. At each step, we calculate mid and compare arr[mid] with target. If equal, floor and ceil are the same. If arr[mid] is less, update floor and move low up. If arr[mid] is greater, update ceil and move high down. The loop ends when low passes high, returning the closest floor and ceil values.