Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Implementation of lower_bound() and upper_bound() on Map of Pairs in C++
Next article icon

Implementation of lower_bound() and upper_bound() in Array of Pairs in C++

Last Updated : 01 Aug, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article we will discuss the implementation of the lower_bound() and upper_bound() in an array of pairs.

  • lower_bound(): It returns an iterator pointing to the first element in the range [first, last) which has a value greater than or equals to the given value “val”. But in Array of Pairs lower_bound() for pair(x, y) will return an iterator pointing to the position of pair whose the first value is greater than or equals x and second value is greater than equals to y.
    If the above-mentioned criteria are not met, then it returns an iterator to the index which out of the array of pairs.
  • upper_bound(): It returns an iterator pointing to the first element in the range [first, last) which has a value greater than the given value “val”. But in Array of Pairs upper_bound() for pair(x, y) will return an iterator pointing to the position of pair whose the first value is greater than x and second value is greater than y.
    If the above-mentioned criteria are not met, then it returns an iterator to the index which out of the array of pairs.

Syntax:

// For lower bound
lower_bound(array_name, array_name + array_size, value, comparator_function);

// For upper bound
upper_bound(array_name, array_name + array_size, value, comparator_function);

Parameters: The function lower_bound() and upper_bound() in array of pairs accepts the following parameters:

  1. array_name & array_size: The name and size of the array which represents the interval between [start, end).
  2. value: Value of the lower_bound()/upper_bound() to be searched in the range.
  3. comparator_function: Binary function that accepts two arguments as its input, namely an element of type pair from the array, and the second is the value for which lower_bound()/upper_bound() has to be found and returns a boolean value.

Return Type: It returns an iterator pointing to the first element of the array whose first parameter is greater than or equal to the value.

Below is the program to demonstrate lower_bound() and upper_bound() in array of pairs:

C++




// C++ program to demonstrate lower_bound()
// and upper_bound() in Array of Pairs
#include <bits/stdc++.h>
using namespace std;
  
// Function to implement lower_bound()
void findLowerBound(pair<int, int> arr[],
                    pair<int, int>& p,
                    int n)
{
    // Given iterator points to the
    // lower_bound() of given pair
    auto low = lower_bound(arr, arr + n, p);
  
    cout << "lower_bound() for {2, 5}"
         << " is at index: "
         << low - arr << endl;
}
  
// Function to implement upper_bound()
void findUpperBound(pair<int, int> arr[],
                    pair<int, int>& p,
                    int n)
{
    // Given iterator points to the
    // lower_bound() of given pair
    auto up = upper_bound(arr, arr + n, p);
  
    cout << "upper_bound() for {2, 5}"
         << " is at index: "
         << up - arr << endl;
}
  
// Driver Code
int main()
{
    // Given sorted array of Pairs
    pair<int, int> arr[]
        = { { 1, 3 }, { 1, 7 }, { 2, 4 },
            { 2, 5 }, { 3, 8 }, { 8, 6 } };
  
    // Given pair {2, 5}
    pair<int, int> p = { 2, 5 };
  
    // Size of array
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call to find lower_bound
    // of pair p in arr
    findLowerBound(arr, p, n);
  
    // Function Call to find upper_bound
    // of pair p in arr
    findUpperBound(arr, p, n);
    return 0;
}
 
 
Output:
  lower_bound() for {2, 5} is at index: 3  upper_bound() for {2, 5} is at index: 4  


Next Article
Implementation of lower_bound() and upper_bound() on Map of Pairs in C++

S

shreyasshetty788
Improve
Article Tags :
  • Arrays
  • C++ Programs
  • DSA
  • cpp-array
  • cpp-pair
Practice Tags :
  • Arrays

Similar Reads

  • Implementation of lower_bound() and upper_bound() on Map of Pairs in C++
    Prerequisite: map lower_bound() function in C++ STL, map upper_bound() function in C++ STL In this article, we will discuss the implementation of the lower_bound() and upper_bound() in the Map of pairs. lower_bound(): It returns an iterator pointing to the first element in the range [first, last) wh
    3 min read
  • 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 set is a policy based data s
    3 min read
  • How to Find the Second Smallest Element in an Array in C++?
    In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element
    3 min read
  • Concatenate Two int Arrays into One Larger Array in C++
    In C++, an array is a linear data structure we use to store data in contiguous order. It helps to efficiently access any element in O(1) time complexity using its index. In this article, we will learn how to concatenate two integer arrays into one larger array in C++. Example: Input: arr1: {1, 2, 3,
    3 min read
  • How to Insert an Element in a Sorted Vector in C++?
    In C++, inserting element in a sorted vector should be done such that it preserves the order of elements. In this article, we will learn different methods to insert an element in a sorted vector in C++. The recommended way to insert an element is to first find the position of insertion using lower_b
    4 min read
  • C++ Program to Find the Second Largest Element in an Array
    In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The
    3 min read
  • How to Sort an Array in Descending Order using STL in C++?
    Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn how to sort an array in descending order using STL in C++. Examples Input: arr[] = {11, 9, 45, 21};Output: 78
    4 min read
  • Find k ordered pairs in array with minimum difference d
    Given an array arr[] and two integers K and D, the task is to find exactly K pairs (arr[i], arr[j]) from the array such that |arr[i] - arr[j]| ? D and i != j. If it is impossible to get such pairs then print -1. Note that a single element can only participate in a single pair.Examples: Input: arr[]
    6 min read
  • How to Find the Range of Numbers in an Array in C++?
    The range of numbers in an array means the difference between the maximum value and the minimum value in the given array. In this article, we will learn how to find the range of numbers in an array in C++. For Example, Input: myArray = {5,10,15,30,25}; Output: Range: 25Range Within an Array in C++To
    2 min read
  • C++ Program to Find the Minimum and Maximum Element of an Array
    Given an array, write functions to find the minimum and maximum elements in it.  Example: C/C++ Code // C++ program to find minimum (or maximum) element // in an array. #include <bits/stdc++.h> using namespace std; int getMin(int arr[], int n) { int res = arr[0]; for (int i = 1; i < n; i++)
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences