Earlier this week, I received an email message from HackerRank suggesting a C++ challenge. Finally I was able to work on it. This past week I was busy with work.
If interested, please take a look at the requirements for the Lower Bound – STL challenge and solve it. When done continue reading to see if our solutions are quite different. If so, please leave me a message in the area provided towards the end of this post.
Following is a screen capture of a console using the sample input for the challenge:
8 1 1 2 2 6 9 9 15 4 1 Yes 1 4 No 5 9 Yes 6 15 Yes 8
Following is the C++ code I generated using Visual Studio 2017 Enterprise Edition IDE:
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; /* */ int main() { // **** get tshe number of elements in the vector **** int N = 0; cin >> N; //cout << "main << N: " << N << endl; // **** declare the vector of integers **** vector<int> vv(N); // **** populate the vector **** for (int n = 0; n < N; n++) { cin >> vv.at(n); //cout << "main <<< vv[" << n << "]: " << vv.at(n) << endl; } // **** get the number of queries **** int Q = 0; cin >> Q; //cout << "main <<< Q: " << Q << endl; // **** read and process queries **** int query = 0; for (int q = 0; q < Q; q++) { // **** read query **** cin >> query; //cout << "main <<< query: " << query << endl; // **** process query **** //vector<int>::iterator low, up; vector<int>::iterator low; low = lower_bound(vv.begin(), vv.end(), query); //up = upper_bound(vv.begin(), vv.end(), query); // **** display result of query **** //cout << "main <<< low: " << low - vv.begin() << endl; //cout << "main <<< up: " << up - vv.begin() << endl; if (vv.at(low - vv.begin()) == query) { cout << "Yes "; } else { cout << "No "; } cout << low - vv.begin() + 1 << endl; } // **** **** return 0; }
The code passed all 17 test cases.
If you have comments or questions please let me know.
Enjoy;
John
Follow me on Twitter: @ john_canessa