Lower Bound-STL

Sort by

recency

|

428 Discussions

|

  • + 0 comments

    // cleanest solution IMO

    int main() {
        int n, q; std::cin >> n;
        std::vector<int> vec(n);
        for (auto &i : vec) std::cin >> i;
        
        std::cin >> q;
        for (int i = 0; i < q; i++) {
            int n; std::cin >> n;
            auto it = std::lower_bound(vec.begin(), vec.end(), n);
            if (*it == n)
                std::cout << "Yes ";
            else
                std::cout << "No ";
            std::cout << (it - vec.begin()) + 1 << '\n';  
        }
        
        return 0;
    }
    
  • + 0 comments

    This one worked for me.

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int N{0}; cin >> N;

    vector<int>vec;
    vec.reserve(N);
    
    int num;
    for(int i{0}; i<N; ++i){
        cin >> num;       
        vec.push_back(num);        
    }
    
    int Q{0};
    cin >> Q;
    vector<int>query;
    query.reserve(Q);
    
    for(int i{0}; i<Q; ++i){
        cin >> num;       
        query.push_back(num);        
    }
    
    int Y{0}, index{0};    
    for(int i{0}; i<Q; ++i){
        Y = query.at(i);
    
        auto it = lower_bound(vec.begin(), vec.end(), Y);
        index = distance(vec.begin(), it);
    
        if(vec[index] == Y)
            cout << "Yes " << (index + 1) << endl; 
        else
             cout << "No " << (index + 1) << endl; 
    }
    
    return 0;
    

    }

  • + 0 comments
    #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 */   
        
        //taking input
        int n{};
        cin >> n;
        vector<int> v;
        for (int i{0}; i < n; ++i)
        {
            int t{};
            cin >> t;
            v.push_back(t);
        }
        // s is number of queries
        int s;
        cin >> s;
        // foreach query, get the value and print necessary value
        for (int i{0}; i < s; ++i)
        {
            int num{}; cin >> num;
            // lower bound returns an iterator type variable
            vector<int>::iterator x;
            x = lower_bound(v.begin(), v.end(), num);
            // getting the index of the lowerbound in the vector
            int pos = x - v.begin(); 
            // if value of lb = the query num, then yes it exists
            // in the vector exists
            if (v[pos] == num) cout << "Yes "<< pos+1;
            else cout << "No "<< pos+1;
            cout << endl;
        }
        
        return 0;
    }
    
  • + 0 comments
    #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 */   
         int n;
        cin >> n;
    
        vector<int> v(n);
        for (int i = 0; i < n; i++) {
            cin >> v[i];
        }
    
        int q;
        cin >> q; 
    
        for (int i = 0; i < q; i++) {
            int x;
            cin >> x; 
    
            vector<int>::iterator low = lower_bound(v.begin(), v.end(), x);
    
            if (v[low - v.begin()] == x) {
                cout << "Yes " << (low - v.begin() + 1) << endl; 
            } else {
                cout << "No " << (low - v.begin() + 1) << endl; 
            }
        }
        return 0;
    }
    
  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        int n, n1;
    
        vector<int> vec;
        
        std::cin >> n;
        
        for (int i=0; i<n; i++)
        {
            std::cin >> n1;
    
            vec.push_back(n1);
        }
        
        std::cin >> n;
    
        for (int i=0; i<n; i++)
        {
            std::cin >> n1;
            
            auto result = std::lower_bound(vec.begin(), vec.end(), n1);
            
            std::cout << (*result == n1 ? "Yes " : "No ") << (result - vec.begin() + 1) << endl;
        }
        
        return 0;
    }