# Floor and ceil of an element in an array using C++

In this tutorial, we will learn how to find floor and ceil of an element in an array using C++? By Radib Kar Last updated : August 10, 2023

Here, we will be searching the floor and ceil of an element from an array. A ceil is the one that is the closest greater number from the array for the element. A floor is the one that is the closest lesser number from the array for the element.

We can solve this method with both linear & binary search.

## By using linear search

Initialize ceil with some largest integer (*INT_MAX*) & floor with some smallest integer (*INT_MIN*).

We can iterate through the array and check if the element is greater than the element & less than the current ceil value, we can update the ceiling value. Similarly, check if the element is lesser than the element & greater than the current floor value, we can update the floor value.

## By using binary search

While doing the normal binary search keep updating the current floor & ceil value with the pivot element (*arr[mid]*) as per possibility.

## C++ program to find floor and ceil of an array element

#include <bits/stdc++.h> using namespace std; //print ceil & floor for the number void ceilandfloorLinear(vector<int>& arr, int k) { cout << "..............Using linear search......\n"; //find the closest greater than number & closet lesser number int _ceil = INT_MAX; int _floor = INT_MIN; for (int i = 0; i < arr.size(); i++) { if (arr[i] > k && _ceil > arr[i]) _ceil = arr[i]; if (arr[i] < k && _floor < arr[i]) _floor = arr[i]; } if (_ceil != INT_MAX) cout << "ceiling of " << k << " is: " << _ceil << "\n"; else cout << "There is no ceiling of " << k << "\n"; if (_floor != INT_MIN) cout << "floor of " << k << " is: " << _floor << "\n"; else cout << "There is no floor of " << k << "\n"; } void ceilandfloorBinary(vector<int>& arr, int k) { cout << "..............Using binary search......\n"; //find the closest greater than number & closet lesser number int _ceil = INT_MAX; int _floor = INT_MIN; sort(arr.begin(), arr.end()); int low = 0, high = arr.size() - 1; while (low <= high) { int mid = (high - low) / 2 + low; if (arr[mid] == k) { _ceil = k; _floor = k; break; } else if (arr[mid] > k) { _ceil = arr[mid]; high = mid - 1; } else { _floor = arr[mid]; low = mid + 1; } } if (_ceil != INT_MAX) cout << "ceiling of " << k << " is: " << _ceil << "\n"; else cout << "There is no ceiling of " << k << "\n"; if (_floor != INT_MIN) cout << "floor of " << k << " is: " << _floor << "\n"; else cout << "There is no floor of " << k << "\n"; } int main() { cout << "Enter number of elements\n"; int n; cin >> n; vector<int> arr(n); cout << "Enter the elements\n"; for (int i = 0; i < n; i++) cin >> arr[i]; cout << "Enter the number for which you need ceil & floor\n"; int k; cin >> k; //using linear search ceilandfloorLinear(arr, k); //using binary search ceilandfloorBinary(arr, k); return 0; }

## Output

Enter number of elements 7 Enter the elements 56 32 87 66 12 59 33 Enter the number for which you need ceil & floor 20 ..............Using linear search...... ceiling of 20 is: 32 floor of 20 is: 12 ..............Using binary search...... ceiling of 20 is: 32 floor of 20 is: 12

Related Tutorials

- Introduction to Algorithms
- Introduction to Greedy Strategy in Algorithms
- Stability in sorting
- External Merge Sorting Algorithm
- Radix Sort and its Algorithm
- Bucket Sort Algorithm
- Bubble sort Algorithm, Flow Chart and C++ Code
- Insertion sort Algorithm, flowchart and C, C++ Code
- Merge Sort | One of the best sorting algorithms used for large inputs
- Binary Search in C, C++
- Randomized Binary Search
- Meta Binary Search | One-sided Binary Search
- Difference between Linear Search and Binary Search
- Binary Search in String
- Variants of Binary Search
- Sieve of Eratosthenes to find prime numbers
- Optimal Merge Pattern (Algorithm and Example)
- Given an array of n numbers, Check whether there is any duplicate or not
- Finding the missing number
- Find the number occurring an odd number of times
- Find the pair whose sum is closest to zero in minimum time complexity
- Find three elements in an array such that their sum is equal to given element K
- Bitonic Search Algorithm
- Check whether a number is Fibonacci or not
- Segregate even and odd numbers in minimum time complexity
- Find trailing zeros in factorial of a number
- Find Nearest Greatest Neighbours of each element in an array
- Interpolation search algorithm
- Two Elements whose sum is closest to zero
- Find a pair with a given difference
- Count number of occurrences (or frequency) in a sorted array
- Find a Fixed Point (Value equal to index) in a given array
- Find the maximum element in an array which is first increasing and then decreasing
- Dynamic Programming (Components, Applications and Elements)
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Dynamic Programming (Components, Applications and Elements)
- Find the Nth Fibonacci number | C++
- Longest Common Subsequence using Dynamic programming (DP)
- Longest Increasing Subsequence using Dynamic programming (DP)
- Find the maximum sub-array sum using KADANE'S ALGORITHM
- Non-intersecting chords using Dynamic Programming (DP)
- Edit Distance using Dynamic Programming (DP)
- Finding Ugly Number using Dynamic Programming (DP)
- Egg dropping problem using Dynamic Programming (DP)
- Wild card matching problem using Dynamic programming (DP)
- Compute sum of digits in all numbers from 1 to N for a given N
- Minimum jumps required using Dynamic programming (DP)
- Graph coloring problem's solution using backtracking algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- Travelling Salesman Problem
- Kruskal's (P) and Prim's (K) Algorithms
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code

- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Find the GCD (Greatest Common Divisor) of two numbers using EUCLID'S ALGORITHM
- Compute the value of A raise to the power B using Fast Exponentiation
- Implement First Come First Served (FCFS) CPU Scheduling Algorithm using C program
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Analysis of LRU page replacement algorithm and Belady's anomaly
- Branch and Bound
- Find the roots of a complex polynomial equation using Regula Falsi Method in C
- Sieve of Eratosthenes to find prime numbers
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Divide and Conquer Paradigm (What it is, Its Applications, Pros and Cons)
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Jump Search Implementation using C++
- Optimal Merge Pattern (Algorithm and Example)
- Introduction to Greedy Strategy in Algorithms
- Strassen's Matrix Multiplication in algorithms
- Huffman Coding (Algorithm, Example and Time complexity)
- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Graph coloring problem's solution using backtracking algorithm
- Tournament Tree and their properties
- Deterministic and Non Deterministic Algorithms
- Lower Bound Theory
- Non Recursive Tree Traversal Algorithm
- Line Drawing Algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- P and NP problems and solutions | Algorithms
- Travelling Salesman Problem
- 2 – 3 Trees Algorithm
- Kruskal's (P) and Prim's (K) Algorithms
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Midpoint Circle Algorithm
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code
- Reliability design problem
- Removing consecutive duplicates from a string
- Fast Exponentiation using Bitmasking

Comments and Discussions!