# Implementing upper_bound() and lower_bound() for Ordered Set in C++

**Prerequisites:** Ordered Set and GNU C++ PBDS

Given an ordered set **set** and a key **K**, the task is to find the upper bound and lower bound of the element **K** in the **set** in C++. If the element is not present or either of the bounds could not be calculated, then print **-1**.

Ordered setis a policy based data structure in g++ that keeps the unique elements in sorted order. It performs all the operations as performed by the set data structure in STL in log(n) complexity. Apart from that, it performs two additional operations also in log(n) complexity like:

1.order_of_key (K):Number of items strictly smaller than K.

2.find_by_order(K):K^{th}element in a set (counting from zero).

**Examples:**

Input:set[] = {10, 20, 30, 40, 50, 60}, K = 30

Output:

Lower Bound of 30: 30

Upper Bound of 30: 40

Explanation:

The lower bound for element 30 is 30 located at position 2

The upper bound for element 30 is 40 located at position 3

Input:set[] = {10, 20, 30, 40, 50, 60}, K = 60

Output:

Lower Bound of 60: -1

Upper Bound of 60: -1

**Approach:**

**upper_bound():**The**upper_bound(key)**function returns the element which is just greater than key passed in parameter.**lower_bound():**The**lower_bound(key)**function returns the element which is equivalent to key passed in the parameter. If the key is not present in the ordered set, then the function should return the element which is greater than the parameter.- In order to implement the upper_bound and lower_bound functions, the index of the element if present in the ordered set passed as the parameter is found using order_of_key() function.
- Now, both the lower bound and the upper bound can be found by simply comparing the elements present at this index.

Below is the implementation of lower_bound() and upper_bound():

`// C++ program to implement the ` `// lower_bound() and upper_bound() ` `// using Ordered Set ` ` ` `#include <bits/stdc++.h> ` `#include <ext/pb_ds/assoc_container.hpp> ` `#include <ext/pb_ds/tree_policy.hpp> ` `using` `namespace` `__gnu_pbds; ` `using` `namespace` `std; ` ` ` `// Ordered Set Tree ` `typedef` `tree<` `int` `, null_type, ` ` ` `less<` `int` `>, ` ` ` `rb_tree_tag, ` ` ` `tree_order_statistics_node_update> ` ` ` `ordered_set; ` ` ` `ordered_set set1; ` ` ` `// Function that returns the lower bound ` `// of the element ` `int` `lower_bound(` `int` `x) ` `{ ` ` ` `// Finding the position of the element ` ` ` `int` `pos = set1.order_of_key(x); ` ` ` ` ` `// If the element is not present in the set ` ` ` `if` `(pos == set1.size()) { ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Finding the element at the position ` ` ` `else` `{ ` ` ` `int` `element ` ` ` `= *(set1.find_by_order(pos)); ` ` ` ` ` `return` `element; ` ` ` `} ` `} ` ` ` `// Function that returns the upper bound ` `// of the element ` `int` `upper_bound(` `int` `x) ` `{ ` ` ` `// Finding the position of the element ` ` ` `int` `pos = set1.order_of_key(x + 1); ` ` ` ` ` `// If the element is not present ` ` ` `if` `(pos == set1.size()) { ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Finding the element at the position ` ` ` `else` `{ ` ` ` `int` `element ` ` ` `= *(set1.find_by_order(pos)); ` ` ` ` ` `return` `element; ` ` ` `} ` `} ` ` ` `// Function to print Upper ` `// and Lower bound of K ` `// in Ordered Set ` `void` `printBound(` `int` `K) ` `{ ` ` ` ` ` `cout << ` `"Lower Bound of "` ` ` `<< K << ` `": "` ` ` `<< lower_bound(K) ` ` ` `<< endl; ` ` ` `cout << ` `"Upper Bound of "` ` ` `<< K << ` `": "` ` ` `<< upper_bound(K) ` ` ` `<< endl; ` `} ` ` ` `// Driver's Code ` `int` `main() ` `{ ` ` ` `set1.insert(10); ` ` ` `set1.insert(20); ` ` ` `set1.insert(30); ` ` ` `set1.insert(40); ` ` ` `set1.insert(50); ` ` ` ` ` `int` `K = 30; ` ` ` `printBound(K); ` ` ` ` ` `K = 60; ` ` ` `printBound(K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Lower Bound of 30: 30 Upper Bound of 30: 40 Lower Bound of 60: -1 Upper Bound of 60: -1

**Time Complexity:** O(log N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Ordered Set and GNU C++ PBDS
- Implementing Multidimensional Map in C++
- Implementing Forward Iterator in BST
- Implementing Backward Iterator in BST
- Number of ordered points pair satisfying line equation
- Implementing Generic Graph in Java
- Implementing a Linked List in Java using Class
- Implementing ternary operator without any conditional statement
- Implementing ANDNOT Gate using Adaline Network
- Implementing Iterator pattern of a single Linked List
- Implementing own Hash Table with Open Addressing Linear Probing in C++
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Find if there is a path between two vertices in a directed graph | Set 2
- Print all possible paths in a DAG from vertex whose indegree is 0

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.