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, 2022 in C++ by Nicholas
• 7,760 points
1,323 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, 2022 by Damon
• 4,960 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, 2022 in C++ by Damon
• 4,960 points
1,144 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

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
403 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

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
356 views
0 votes
0 answers

How to access static members of a class?

I'm learning C++ and Qt, but even the simplest code that I copy and paste from a book produces problems. On Ubuntu 10.04, I'm using g++4.4.2 with the QtCreator IDE.  Is there a distinction between the syntax of the g++ compiler and those of other compilers?  When I try to access static members, for example, something always goes wrong. #include <iostream> using namespace std; class A { ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
236 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, 2022 in C++ by Damon
• 4,960 points
712 views
0 votes
0 answers

C++ vector erase function not working properly

Here is the code I want to use to delete any entries with values more than 2 and fewer than 5. vector<int> myvector{3, 3, 3, 3, 3, 3, ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
431 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, 2022 in Others by Damon
• 4,960 points
11,362 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, 2022 in C++ by Damon
• 4,960 points
2,294 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 Jun 27, 2022 in C++ by Damon
• 4,960 points
516 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, 2022 in C++ by Damon
• 4,960 points
485 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP