Lower Bound-STL in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Lower Bound-STL in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Lower Bound-STL in C++ - Hackerrank Solution

Problem

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 :

 8
 1 1 2 2 6 9 9 15
 4
 1
 4
 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];
   cin>>n;
   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;
       }
       else
       {
           cout << "No " << (low - v.begin() + 1) << endl;
       }
   }
   /* Lower Bound-STL in C++ - Hacker Rank Solution END */
 return 0;
}

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh