0
0
DSA Goprogramming~30 mins

Floor and Ceil in Sorted Array in DSA Go - Build from Scratch

Choose your learning style9 modes available
Floor and Ceil in Sorted Array
📖 Scenario: You are working with a sorted list of house numbers on a street. You want to find the closest house number that is less than or equal to a given number (floor) and the closest house number that is greater than or equal to that number (ceil).
🎯 Goal: Build a program that finds the floor and ceil values for a given number in a sorted array of house numbers.
📋 What You'll Learn
Create a sorted array of integers called houses with exact values
Create an integer variable called target for the number to find floor and ceil
Write a function findFloorAndCeil that takes houses and target and returns two integers: floor and ceil
Print the floor and ceil values in the format: Floor: X, Ceil: Y
💡 Why This Matters
🌍 Real World
Finding floor and ceil values is useful in real estate, pricing, and scheduling where you need closest matches in sorted data.
💼 Career
Understanding floor and ceil concepts helps in algorithm design, data querying, and optimization tasks common in software development.
Progress0 / 4 steps
1
Create the sorted array of house numbers
Create a sorted array of integers called houses with these exact values: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91
DSA Go
Hint

Use houses := []int{2, 5, 8, 12, 16, 23, 38, 56, 72, 91} to create the array.

2
Set the target number to find floor and ceil
Create an integer variable called target and set it to 20
DSA Go
Hint

Use target := 20 to create the variable.

3
Write the function to find floor and ceil
Write a function called findFloorAndCeil that takes houses []int and target int and returns two integers: floor and ceil. Use a simple loop to find the floor (largest number ≤ target) and ceil (smallest number ≥ target).
DSA Go
Hint

Loop through houses. Update floor if house ≤ target. Set ceil to first house ≥ target and break.

4
Print the floor and ceil values
Call findFloorAndCeil with houses and target. Print the floor and ceil values in the format: Floor: X, Ceil: Y
DSA Go
Hint

Use floor, ceil := findFloorAndCeil(houses, target) and fmt.Printf("Floor: %d, Ceil: %d\n", floor, ceil).