The lower bound is a function that can be used with a sorted vector. Learn how to use lower bound to solve this problem by clicking here.
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: 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 <= N <= 10^5, where X is the 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 presently 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
So according to the first query 1 is present in an index 1 so our output is Yes 1, Now move on to the second query 4 is not present in an array that's why the output is No 5, according to the condition if the number is not present in an array then it should return a next greater number than query. Now next query is 9 it is present in an index 6 so the output is Yes 6. and the last query is 15 it is also present in an array that's why the output is Yes 8.
Note: Here an array index is starting with 1, which normally starts with 0.
Submit your solution here: Click here
Lower Bound STL Hackerrank Solution in C++
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: 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 <= N <= 10^5, where X is the 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 presently 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
So according to the first query 1 is present in an index 1 so our output is Yes 1, Now move on to the second query 4 is not present in an array that's why the output is No 5, according to the condition if the number is not present in an array then it should return a next greater number than query. Now next query is 9 it is present in an index 6 so the output is Yes 6. and the last query is 15 it is also present in an array that's why the output is Yes 8.
Note: Here an array index is starting with 1, which normally starts with 0.
Submit your solution here: Click here
Lower Bound STL Hackerrank Solution in C++
aaa
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int m, num;
cin >> m;
vector<int> v;
for (int i = 0; i < m; i++)
{
cin >> num;
v.push_back(num);
}
int n, val;
cin >> n;
for (int 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;
}
return 0;
}
The Output of Lower Bound STL Hackerrank Solution
Similar to Lower Bound STL
- Vector Erase Hackerrank Solution in C++
- Magic Spells Hackerrank Solution in C++
- Vector Sort Hackerrank Solution in C++
- Structs Hackerrank Solution in C++
- Variable Sized Arrays Hackerrank Solution in C++
- Conditional Statements in C++ Hackerrank Solution
- Reverse a Linked List Hackerrank Solution
- Input And Output Hackerrank Solution in C++
- Solve Me First Hackerrank Solution in C & C++
- A Very Big Sum Hackerrank Solution C++
- Deque STL Hackerrank Solution in C++
- Mini Max Sum Hackerrank Solution in C++
- Staircase Hackerrank Solution in C
- Attribute Parser Hackerrank Solution in C++
0 Comments: