Lower Bound-STL in C++ Hacker Rank Solution

Hello coders, In this post, you will learn how to solve the Lower Bound-STL in C++ Hacker Rank Solution. This problem is a part of the HackerRank C++ Programming Series.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

Lower Bound-STL in C++ Hacker Rank Solution
Lower Bound-STL in C++ – Hacker Rank Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology,Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

Lower Bound-STL in C++ Hacker Rank Solution


You are given N integers in sorted order. Also, you are given Q queries. In each query, you will be given an integer and you have to tell whether that integer is present in the array. If so, you have to tell at which index it is present and if it is not present, you have to tell the index at which the smallest integer that is just greater than the given number is present. Lower bound is a function that can be used with a sorted vector. Learn how to use lower bound to solve this problem by

Input Format :

The first line of the input contains the number of integers N. The next line contains N integers in sorted order. The next line contains Q, the number of queries. Then Q lines follow each containing a single integer Y.Note that if the same number is present multiple times, you have to print the first index at which it occurs. Also, the input is such that you always have an answer for each query.

Constraints :

  • 1 <= N <= 10^5
  • 1 <= Xi <= 10^9, where Xi is ith element in the array.
  • 1 <= Q <= 10^5
  • 1 <= Y <= 10^9

Output Format :

For each query you have to print “Yes” (without the quotes) if the number is present and at which index(1-based) it is present separated by a space.
If the number is not present you have to print “No” (without the quotes) followed by the index of the next smallest number just greater than that number.
You have to output each query in a new line.

Sample Input :

 1 1 2 2 6 9 9 15

Sample Output :

 Yes 1
 No 5
 Yes 6
 Yes 8

Lower Bound-STL in C++ Hacker Rank Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
   /* Enter your code here. Read input from STDIN. Print output to STDOUT */
   /* Lower Bound-STL in C++ - Hacker Rank Solution START */
   int m, n, num, i, val;
   cin >>m;
   vector <int> v(m);
   for(i = 0; i < m; i++)
       cin >> v[i];
   for(i = 0; i < n; i++)
       cin >> val;
       vector <int>::iterator low = lower_bound(v.begin(), v.end(), val);
       if (v[low - v.begin()] == val)
           cout << "Yes " << (low - v.begin() + 1) << endl;
           cout << "No " << (low - v.begin() + 1) << endl;
   /* Lower Bound-STL in C++ - Hacker Rank Solution END */
 return 0;

Disclaimer: The above Problem (Lower Bound-STL in C++ ) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.


Which language is best for HackerRank?

While JavaScript is the best-known language, HackerRank also found that only 5% of respondents say it is their first programming language.

Is HackerRank good for beginners?

HackerRank is very good for beginners so even if you want to print your first program “Hello World!” then definitely HackerRank gives this opportunity to you. It has a pretty good UI with boilerplate code pre-written that helps beginners to start competitive coding.

What is meant by HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedia

What is C++ programming used for?

C++ is a general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or “C with Classes”. Wikipedia

Where can I find HackerRank solutions in CPP?

in this post you will get all the solutions of HackerRank CPP Problems.

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe

Sharing Is Caring

Leave a Comment