How do i apply lower bound to a range of unsorted vector elements

0 votes

I have a vector of integer scores = 10, 23, 29, 77, 8, 43, 56, 3, a number predict = 42, and a start index = 2 as well as a number predict = 42.

I'm looking for two values that are (greater than and less than) 42 in the range of start index = 2 and finish index = 7.

scores = 10, 23, 29, 77, 8, 43, 56, 3 (search only inside i=2 to i=7)

As a result, 29 is little less than predict=42.

Also, 43 is a touch higher than the forecasted 42.

How did I come up with these figures?

Sample Code:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> scores = {10, 23, 29, 77, 8, 43, 56, 3}; //Unsorted
    int predict = 42;
    int startFromIndex = 2;
    
    int littleLessThanPredict; // = 29
    int littleMoreThanPredict; // = 43
    
    //lower_bound
    //upper_bound
    
    return 0;
}

In consideration of this scenario,

On an unsorted range, how do I utilise the std::lower bound and std::upper bound functions?

How can I temporarily sort a portion of a vector and apply my own function?

Is there a method to get the desired result by combining the std::min element and std::lower bound functions?

Jun 15 in C++ by Nicholas
• 2,460 points
15 views

1 answer to this question.

0 votes

What's the point of sorting your array? The complexity of the algorithm will be O(N.ln(N)). You can create an O(N) method by combining upper and lower bounds research in an unsorted array:

#include <algorithm>
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

pair<int, int> nearestNumbers(const vector<int> &scores, int predict, int start) {
    int nearestLower = INT_MIN;
    int nearestUpper = INT_MAX;

    for (size_t i = start; i < scores.size(); i++) {
        int value = scores[i];
        if (value <= predict && value > nearestLower)
            nearestLower = value;
        else if (value >= predict && value < nearestUpper)
            nearestUpper = value;
    }

    return pair<int, int>(nearestLower, nearestUpper);
}

int main() {
    vector<int> scores = {10, 23, 29, 77, 8, 43, 56, 3};
    int predict = 42;
    int start = 2;

    pair<int, int> results = nearestNumbers(scores, predict, start);
    cout << results.first << " " << results.second;

    return 0;
}
answered Jun 15 by Damon
• 3,580 points

Related Questions In C++

0 votes
1 answer

How do I erase an element from std::vector<> by index?

You might perform the following to remove a single element: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(std::next(vec.begin())); Alternatively, to remove many elements at once: // ...READ MORE

answered Jun 10 in C++ by Damon
• 3,580 points
14 views
0 votes
0 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

1 day ago in C++ by Nicholas
• 2,460 points
6 views
0 votes
0 answers

How can I get the maximum or minimum value in a vector?

In C++, how can I find the greatest or minimum value in a vector? Is it correct to assume that it would be similar with an array? Do I require an iterator?  I tried max element, but I kept receiving errors. vector<int>::const_iterator it; it = max_element(cloud.begin(), cloud.end()); error: request for ...READ MORE

1 day ago in C++ by Nicholas
• 2,460 points
3 views
0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
25 views
0 votes
1 answer

Lower and Upper Bound in case of Decreasing/Non-ascending vector

Both std::lower bound and std::upper bound must have an increasing (non-decreasing) order as their objective. By giving a comparator as the 4th parameter of the functions, you may modify the meaning of "growing." To work with descending vectors, use std::greater. #include<iostream> #include<vector> #include<algorithm> #include<functional> using namespace std; int main() { ...READ MORE

answered Jun 14 in C++ by Damon
• 3,580 points
6 views
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27 in Others by Damon
• 3,580 points
12 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
33 views
0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1 in C++ by Nicholas
• 2,460 points
11 views
0 votes
1 answer

How do I reverse a C++ vector?

The algorithm header has a method std::reverse for this purpose. #include <vector> #include <algorithm> int main() { std::vector<int> ...READ MORE

answered 1 day ago in C++ by Damon
• 3,580 points
5 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
25 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP